Wxpython program triggers menu or button events, wxpython triggers
Recently, I have gradually become familiar with wxpython and compiled several small GUI programs. There is no need to trigger control events in code in the GUI. In other Gui languages, postevent and triggerevent call the function of the event name, which is very convenient.
How can we solve this problem in wxpython? The last simple code.
Class frame (wx. Frame): ID_Help = wx. NewId ()
Def _ init _ (self, frame ):
Super (TaskBarIcon, self). _ init __()
Self. frame = frame
Self. Bind (wx. EVT_MENU, self. OnHelp, id = self. ID_Help)
......
Self. Bind (wx. EVT_CHECKBOX, self. OnShowDetail, self. cbxShowDetail) self. Bind (wx. EVT_BUTTON, self. OnPrint, self. btnPrint )...... Def CreatePopupMenu (self): menu = wx. Menu () menu. Append (self. ID_Help, U' help & F1 ') return menu ......
Def ShowMain ():
# Trigger menu event, id = self. ID_Help
IRet = wx. PostEvent (self, wx. CommandEvent (wx. EVT_MENU.typeId, self. ID_Help ))
Return iRet
Def PrintMain ():
# Trigger button event, id = self. btnPrint. GetId ()
# It is equivalent to executing the OnPrint () event bound to btnPrint.
IRet = wx. PostEvent (self, wx. CommandEvent (wx. EVT_BUTTON.typeId, self. btnPrint. GetId ()))
Return iRet
Def PrintMain ():
# Trigger the checkbox event, id = self. cbxShowDetail. GetId ()
# The Event OnShowDetail () bound to cbxShowDetail is executed.
Self. cbxShowDetail. SetValue (True)
IRet = wx. PostEvent (self, wx. CommandEvent (wx. EVT_CHECKBOX.typeId, self. cbxShowDetail. GetId ()))
Return iRet
Explanation:
Wx. PostEvent (self, wx. CommandEvent (wx. EVT_CHECKBOX.typeId, self. cbxShowDetail. GetId ()))
Parameter 1. self indicates the window handle for processing postevent.
Parameter 2, event = wx. CommandEvent (eventtype, eventid)
Eventtype: wx. evt_menu, wx. evt_button, and wx. evt_checkboxx.
Eventid is the id of the control bound to the event.
Above.