Event handling is the basic mechanism for Wxpython programs to work, first look at several terms:
- Event: Something that should occur during the program and requires a response.
- Event object: Represents a specific event, including the data property of the event, for WX. An instance of an event or its subclasses, such as Wx.commandevent/wx.mouseevent.
- Event Type: Wxpython an integer ID assigned to each event object
Wx. Sub-class of event:
- Wx. Closeevent: Triggered when the framework is closed, the event type has normal frame shutdown and system shutdown events.
- Wx.commandevent: Simple interaction with widgets triggers this event, such as button click, menu item selection, and so on.
- Wx. KeyEvent: Key action.
- Wx. MouseEvent: Mouse event.
- Wx. PaintEvent: Triggered when the window content is redrawn.
- Wx. Sizeevent: Triggered when the window size or layout changes.
- Wx. TimerEvent: by class WX. Timer creation, recurring event.
Wx. Evthandler's Bind method:
It is used to create event bindings, which are prototyped as follows:
Bind (event, Handler, Source=none, Id=wx.id_any, Id2=wx.id_any)
It binds an event and an object to an event handler function.
Look at a menu item select an instance of event binding:
#!/usr/bin/Env python#-*-coding:utf-8-*-" "Function: Common dialog box Instance input:none output:none author:socrates blog:http://www.cnblogs.com/dyx1024/Date -- -- -" " Import Wximport wx.py.imagesclassToolbarframe (WX. Frame): Def __init__ (self, Parent, id): WX. Frame.__init__ (self, parent, ID,'Toolbars', size = ( -, $)) 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 () #菜单内容&indicates that the following character is a hotkey, and Parameter 3 is the menu item description displayed on the status bar Menu_item1= Menu2. Append (WX. NewId (),"©","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"message dialog box Test", u"Title Information"Wx. Yes_no |WX. Icon_question)ifDlg. 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 () #!/usr/bin/Env python#-*-coding:utf-8-*-" "Function: Common dialog box Instance input:none output:none author:socrates blog:http://www.cnblogs.com/dyx1024/Date -- -- -" " Import Wximport wx.py.imagesclassToolbarframe (WX. Frame): Def __init__ (self, Parent, id): WX. Frame.__init__ (self, parent, ID,'Toolbars', size = ( -, $)) 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 () #菜单内容&indicates that the following character is a hotkey, and Parameter 3 is the menu item description displayed on the status bar Menu_item1= Menu2. Append (WX. NewId (),"©","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"message dialog box Test", u"Title Information"Wx. Yes_no |WX. Icon_question)ifDlg. 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: Event Handling One