2019年12月15日
一.安装
推荐用pip安装 (官网也是用这个安装的)()
pip install -U wxPython
![](https://img.haomeiwen.com/i3003454/6db3967fc99021f4.png)
之后就正常了
![](https://img.haomeiwen.com/i3003454/7b9bff5d85f1f10e.png)
2.ps demo下载 (https://extras.wxpython.org/wxPython4/extras/)
直接运行demo报错
二.不推荐用如下方法,好像装出3.7版本了
1安装 (https://www.jianshu.com/p/111b4bcc6148)
1.brew install wxpython (大概装了4个小时)
![](https://img.haomeiwen.com/i3003454/50f9bc73f7835a79.png)
![](https://img.haomeiwen.com/i3003454/383ad5ba50d0c59b.png)
2.ps demo下载 (https://extras.wxpython.org/wxPython4/extras/)
直接运行demo报错
![](https://img.haomeiwen.com/i3003454/b95da892b0e9784b.png)
解决:需要链接
/usr/local/lib/python3.7/site-packages
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
![](https://img.haomeiwen.com/i3003454/29b7b6d5ca50606c.png)
sudo ln -s /usr/local/Cellar/wxpython/4.0.7.post2/libexec/lib/python3.7/site-packages/wx wx
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
三.wxPython基础
1.窗口类
![](https://img.haomeiwen.com/i3003454/41b913df942e5f82.png)
![](https://img.haomeiwen.com/i3003454/b9758f03d38bc38b.png)
1.一般例子
import wx
# 创建应用程序对象
app = wx.App()
# 创建窗口对象
frm = wx.Frame(None, title="第一个GUI程序!", size=(400, 300), pos=(100, 100))
frm.Show() # 显示窗口
app.MainLoop() # 进入主事件循环
![](https://img.haomeiwen.com/i3003454/d0af9ac11fcf0e14.png)
2.通用例子
![](https://img.haomeiwen.com/i3003454/90661dbc5d08247c.png)
效果
![](https://img.haomeiwen.com/i3003454/e5d79f40656f63ed.png)
import wx
# 自定义窗口类MyFrame
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="第一个GUI程序!", size=(400, 300))
self.Centre() # 设置窗口居中
panel = wx.Panel(parent=self)
statictext = wx.StaticText(parent=panel, label='Hello World!', pos=(10, 10))
class App(wx.App):
def OnInit(self):
# 创建窗口对象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 进入主事件循环
四.事件处理:
![](https://img.haomeiwen.com/i3003454/51e52746c1fda44b.png)
1.1对1事件处理
![](https://img.haomeiwen.com/i3003454/985db70d0d296476.png)
实现:
import wx
# 自定义窗口类MyFrame
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='一对一事件处理', size=(300, 180))
self.Centre() # 设置窗口居中
panel = wx.Panel(parent=self)
self.statictext = wx.StaticText(parent=panel, pos=(110, 20))
b = wx.Button(parent=panel, label='OK', pos=(100, 50))
self.Bind(wx.EVT_BUTTON, self.on_click, b)
def on_click(self, event):
print(type(event)) # <class 'wx._core.CommandEvent'>
self.statictext.SetLabelText('Hello, world.')
class App(wx.App):
def OnInit(self):
# 创建窗口对象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 进入主事件循环
2.1对多事件
![](https://img.haomeiwen.com/i3003454/700494fa29c04b4c.png)
import wx
# 自定义窗口类MyFrame
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='一对多事件处理', size=(300, 180))
self.Centre() # 设置窗口居中
panel = wx.Panel(parent=self)
self.statictext = wx.StaticText(parent=panel, pos=(110, 15))
b1 = wx.Button(parent=panel, id=10, label='Button1', pos=(100, 45))
b2 = wx.Button(parent=panel, id=11, label='Button2', pos=(100, 85))
# self.Bind(wx.EVT_BUTTON, self.on_click, b1)
# self.Bind(wx.EVT_BUTTON, self.on_click, id=11)
self.Bind(wx.EVT_BUTTON, self.on_click, id=10, id2=20)
def on_click(self, event):
event_id = event.GetId()
print(event_id)
if event_id == 10:
self.statictext.SetLabelText('Button1单击')
else:
self.statictext.SetLabelText('Button2单击')
class App(wx.App):
def OnInit(self):
# 创建窗口对象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 进入主事件循环
3.鼠标事件处理
![](https://img.haomeiwen.com/i3003454/b0e53ffd805338bd.png)
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="鼠标事件处理", size=(400, 300))
self.Centre() # 设置窗口居中
self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
self.Bind(wx.EVT_LEFT_UP, self.on_left_up)
self.Bind(wx.EVT_MOTION, self.on_mouse_move)
def on_left_down(self, evt):
print('鼠标按下')
def on_left_up(self, evt):
print('鼠标释放')
def on_mouse_move(self, event):
if event.Dragging() and event.LeftIsDown():
pos = event.GetPosition()
print(pos)
class App(wx.App):
def OnInit(self):
# 创建窗口对象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 进入主事件循环
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。
网友评论