2019年12月15日
一.安裝
推薦用pip安裝 (官網(wǎng)也是用這個(gè)安裝的)()
pip install -U wxPython

image.png
之后就正常了

image.png
2.ps demo下載 (https://extras.wxpython.org/wxPython4/extras/)
直接運(yùn)行demo報(bào)錯(cuò)
二.不推薦用如下方法,好像裝出3.7版本了
1安裝 (http://www.itdecent.cn/p/111b4bcc6148)
1.brew install wxpython (大概裝了4個(gè)小時(shí))

image.png

image.png
2.ps demo下載 (https://extras.wxpython.org/wxPython4/extras/)
直接運(yùn)行demo報(bào)錯(cuò)

image.png
解決:需要鏈接
/usr/local/lib/python3.7/site-packages
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

image.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基礎(chǔ)
1.窗口類

image.png

image.png
1.一般例子
import wx
# 創(chuàng)建應(yīng)用程序?qū)ο?app = wx.App()
# 創(chuàng)建窗口對(duì)象
frm = wx.Frame(None, title="第一個(gè)GUI程序!", size=(400, 300), pos=(100, 100))
frm.Show() # 顯示窗口
app.MainLoop() # 進(jìn)入主事件循環(huán)

image.png
2.通用例子

image.png
效果

image.png
import wx
# 自定義窗口類MyFrame
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="第一個(gè)GUI程序!", size=(400, 300))
self.Centre() # 設(shè)置窗口居中
panel = wx.Panel(parent=self)
statictext = wx.StaticText(parent=panel, label='Hello World!', pos=(10, 10))
class App(wx.App):
def OnInit(self):
# 創(chuàng)建窗口對(duì)象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 進(jìn)入主事件循環(huán)
四.事件處理:

image.png
1.1對(duì)1事件處理

image.png
實(shí)現(xiàn):
import wx
# 自定義窗口類MyFrame
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='一對(duì)一事件處理', size=(300, 180))
self.Centre() # 設(shè)置窗口居中
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):
# 創(chuàng)建窗口對(duì)象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 進(jìn)入主事件循環(huán)
2.1對(duì)多事件

image.png
import wx
# 自定義窗口類MyFrame
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='一對(duì)多事件處理', size=(300, 180))
self.Centre() # 設(shè)置窗口居中
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):
# 創(chuàng)建窗口對(duì)象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 進(jìn)入主事件循環(huán)
3.鼠標(biāo)事件處理

image.png
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="鼠標(biāo)事件處理", size=(400, 300))
self.Centre() # 設(shè)置窗口居中
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('鼠標(biāo)按下')
def on_left_up(self, evt):
print('鼠標(biāo)釋放')
def on_mouse_move(self, event):
if event.Dragging() and event.LeftIsDown():
pos = event.GetPosition()
print(pos)
class App(wx.App):
def OnInit(self):
# 創(chuàng)建窗口對(duì)象
frame = MyFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop() # 進(jìn)入主事件循環(huán)
如果您發(fā)現(xiàn)本文對(duì)你有所幫助,如果您認(rèn)為其他人也可能受益,請(qǐng)把它分享出去。