Python图形用户界面开发工具包, 主要提供如下GUI内容:
- 窗口;
- 控件;
- 事件处理;
- 布局管理。
wxPython窗口主要方法:
- 窗口类:wx.Frame()
- 画板:wx.Panel()
- 应用程序类:wx.App()
- OnInit() --该方法在应用程序启动时调用,可以在此方法中进行应用程序的初始化,该方法返回值是布尔类型,True:继续运行应用,False:立刻退出应用。
- OnExit() --该方法在应用程序退出时调用,可以在此方法中释放一些资源,如数据库连接等。
- show() --显示窗口。
- app.Mainloop() --进入主事件循环。
import wx
# 自定义窗口类MyFrame
class MyFrame(wx.Frame):
'继承wx.Fram类'
def __init__(self):
# super().__init__(parent=None, title='自定义窗口', size=(400, 300), pos=(100, 100))
super().__init__(parent=None, title='自定义窗口', size=(400, 300))
# 设置窗口居中
self.Center()
# 创建面板对象
panel = wx.Panel(parent=self)
static_text = wx.StaticText(parent=panel, label='Hello Python', pos=(10, 10))
# 自定义应用程序类MyApp
class MyApp(wx.App):
'继承wx.App类'
def OnInit(self):
# 创建窗口对象
frame = MyFrame()
frame.Show()
return True
def OnExit(self):
print('应用程序退出..')
return 0
if __name__ == '__main__':
app = MyApp()
app.MainLoop()
执行结果:
wxPython事件处理主要方法:
Bind(event,handler,source,id) --绑定是通过事件处理类的Bind()方法实现的。
- event --事件类型,注意不是事件。
- handler --事件处理者。
- source --事件源。
- id --事件源的标识。
Unbind() --解除绑定。
将事件分为一对一事件、一对多事件处理以及鼠标滑动事件分析。
# 一对一事件
import wx
class myFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='一对一事件处理', size=(300, 180))
self.Center()
pancel = wx.Panel(parent=self)
self.staticText = wx.StaticText(parent=pancel, pos=(110, 20))
showTextButt = wx.Button(parent=pancel, label='显示静态文本', pos=(100, 50))
self.Bind(wx.EVT_BUTTON, self.showText, showTextButt)
def showText(self, event):
self.staticText.SetLabelText('Hello Python')
class myApp(wx.App):
'窗口对象'
def OnInit(self):
frame = myFrame()
frame.Show()
return True
def OnExit(self):
print('程序退出。。')
return 0
if __name__ == '__main__':
app = myApp()
app.MainLoop()
执行结果:
# 一对多事件处理
import wx
class MyFrame(wx.Frame):
'自定义窗口类'
def __init__(self):
super().__init__(parent=None, title='一对多事件处理', size=(300, 200))
self.Center()
panel = wx.Panel(parent=self)
self.static_Text = wx.StaticText(parent=panel, pos=(110, 15))
b1 = wx.Button(parent=panel, id=10, label='Button_1', pos=(100, 45))
b2 = wx.Button(parent=panel, id=11, label='Button_2', pos=(100, 85))
# self.Bind(wx.EVT_BUTTON, self.on_click, b1)
# self.Bind(wx.EVT_BUTTON, self.on_click, b2)
# 上面两句可以绑定为下面一句:
self.Bind(wx.EVT_BUTTON, self.on_click, id=10, id2=11)
def on_click(self, event):
'按钮事件'
event_id = event.GetId()
# print(event_id)
if event_id == 10:
self.static_Text.SetLabelText('Button_1 Click Me.')
elif event_id == 11:
self.static_Text.SetLabelText('Button_2 Click Me.')
else:
print('Button Id Error')
class MyApp(wx.App):
def OnInit(self):
fram = MyFrame()
fram.Show()
return True
def OnExit(self):
print('Program exits..')
return 0
if __name__ == '__main__':
app = MyApp()
app.MainLoop()
# 鼠标事件处理
import wx
class myFrame(wx.Frame):
'自定义窗口'
def __init__(self):
super().__init__(parent=None, title='鼠标事件处理', size=(400, 300))
self.Center()
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('on_left_down.')
def on_left_up(self, evt):
'鼠标释放'
print('on_left_up')
def on_mouse_move(self, evt):
'鼠标滑动'
if evt.Dragging() and evt.LeftIsDown():
pos = evt.GetPosition()
print(pos)
class myApp(wx.App):
'自定义应用程序'
def OnInit(self):
frame = myFrame()
frame.Show()
return True
def OnExit(self):
print('Program exits.')
return 0
if __name__ == '__main__':
app = myApp()
app.MainLoop()
执行结果: