事件处理是wxPython程序工作的基本机制,先看几个术语:
- 事件(event):应该程序期间发生的事情,要求有一个响应。
- 事件对象(event object):代表具体一个事件,包括事件的数据属性,为wx.Event或其子类的实例,如wx.CommandEvent/wx.MouseEvent。
- 事件类型(event type):wxPython分配给每个事件对象的一个整数ID。
事件驱动模型:

wx.Event的子类:
- wx.CloseEvent:框架关闭时触发,事件类型有普通框架关闭和系统关闭事件。
- wx.CommandEvent:与窗口部件的简单的交互都会触发此事件,如按钮单击,菜单项选择等。
- wx.KeyEvent:按键动作。
- wx.MouseEvent:鼠标事件。
- wx.PaintEvent:当窗口内容被重画时触发。
- wx.SizeEvent:窗口大小或布局改变时触发。
- wx.TimerEvent:由类wx.Timer创建,定期事件。
wx.EvtHandler的Bind方法:
它用来创建事件绑定,原型如下:
Bind(event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY)
它将一个事件和一个对象与一个事件处理函数绑定。
看一个菜单项选择事件绑定的实例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Function:常用对话框实例
Input:NONE
Output: NONE
author: socrates
blog:http://www.cnblogs.com/dyx1024/
date:2012-07-07
'''
import wx
import wx.py.images
class ToolbarFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Toolbars', size = (300, 200))
panel = wx.Panel(self)
panel.SetBackgroundColour('White')
#创建状态栏
statusBar = self.CreateStatusBar()
#创建工具栏
toolbar = self.CreateToolBar()
#增加一个工具
toolbar.AddSimpleTool(wx.NewId(), wx.py.images.getPyBitmap(), "New", "Long help for 'New'")
toolbar.AddSimpleTool(wx.NewId(), wx.py.images.getPyBitmap(), "Edit", "Long help for 'Edit'")
#准备显示
toolbar.Realize()
#创建菜单
menuBar = wx.MenuBar()
menu1 = wx.Menu()
menuBar.Append(menu1, "&File") #菜单项目1
menu2 = wx.Menu()
#菜单内容&表示随后的字符为热键,参数3为在状态栏上显示的菜单项说明
menu_item1 = menu2.Append(wx.NewId(), "&Copy", "Copy in status bar")
menu2.Append(wx.NewId(), "C&ut", "")
menu2.Append(wx.NewId(), "Paste", "")
menu2.AppendSeparator()
menu2.Append(wx.NewId(), "&Options...", "Display Options")
menuBar.Append(menu2, "&Edit")
self.SetMenuBar(menuBar)
#菜单项事件绑定
self.Bind(wx.EVT_MENU, self.OnCloseMe, menu_item1)
#消息对话框
def OnCloseMe(self, event):
dlg = wx.MessageDialog(None, u"消息对话框测试", u"标题信息", wx.YES_NO | wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
self.Close(True)
dlg.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = ToolbarFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()
测试一下:


事件处理流程,如下:

wxPython首先在触发对象中查找匹配事件类型的被绑定的处理器函数,如果找到,刚相应方法被执行。如果没找到,wxPython将检查该事件是否传送到了上一级的容器,如果是,父窗口被检查,如此一级级向上查找,直到找到一个处理函数或到达顶层窗口。
看一个触发多个事件的实例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Function:常用对话框实例
Input:NONE
Output: NONE
author: socrates
blog:http://www.cnblogs.com/dyx1024/
date:2012-07-07
'''
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, u'测试面板Panel', size = (600, 300))
#创建面板
self.panel = wx.Panel(self)
#在Panel上添加Button
self.button = wx.Button(self.panel, label = u'关闭', pos = (150, 60), size = (100, 60))
#绑定单击事件
self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.button)
#绑定鼠标位于按钮上事件
self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindows)
#绑定鼠标离开事件
self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindows)
def OnCloseMe(self, event):
self.panel.SetBackgroundColour('Red')
self.panel.Refresh()
def OnEnterWindows(self, event):
self.panel.SetBackgroundColour('Blue')
self.panel.Refresh()
self.button.SetLabel(u"鼠标在我上面")
event.Skip()
def OnLeaveWindows(self, event):
self.panel.SetBackgroundColour('Green')
self.panel.Refresh()
self.button.SetLabel(u"鼠标离开我了")
event.Skip()
# #消息对话框
# def OnCloseMe(self, event):
# dlg = wx.MessageDialog(None, u"消息对话框测试", u"标题信息", wx.YES_NO | wx.ICON_QUESTION)
# if dlg.ShowModal() == wx.ID_YES:
# self.Close(True)
# dlg.Destroy()
#
# #文本输入对话框
# def OnCloseMe(self, event):
# dlg = wx.TextEntryDialog(None, u"请在下面文本框中输入内容:", u"文本输入框标题", u"默认内容")
# if dlg.ShowModal() == wx.ID_OK:
# message = dlg.GetValue() #获取文本框中输入的值
# dlg_tip = wx.MessageDialog(None, message, u"标题信息", wx.OK | wx.ICON_INFORMATION)
# if dlg_tip.ShowModal() == wx.ID_OK:
# self.Close(True)
# dlg_tip.Destroy()
# dlg.Destroy()
#列表选择对话框
# def OnCloseMe(self, event):
# dlg = wx.SingleChoiceDialog(None, u"请选择你喜欢的水果:", u"列表选择框标题",
# [u"苹果", u"西瓜", u"草莓"])
# if dlg.ShowModal() == wx.ID_OK:
# message = dlg.GetStringSelection() #获取选择的内容
# dlg_tip = wx.MessageDialog(None, message, u"标题信息", wx.OK | wx.ICON_INFORMATION)
# if dlg_tip.ShowModal() == wx.ID_OK:
# self.Close(True)
# dlg_tip.Destroy()
# dlg.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()
测试一下:
1、初始运行:

2、鼠标移动到按钮上:

3、鼠标左键单击:

4、鼠标离开按钮:


浙公网安备 33010602011771号