最近逐步熟悉wxpython,編寫了幾個小小功能的GUI程序,GUI中免不了會有在代碼中觸發控件事件的業務需求。在其他Gui界面的語言中有postevent、triggerevent 調用事件名稱的函數,非常方便。
在wxpython里如何解決呢,上一段簡單的代碼。
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'幫助&F1')
return menu
……
def ShowMain():
# 觸發菜單事件,id=self. ID_Help
iRet = wx.PostEvent(self,wx.CommandEvent(wx.EVT_MENU.typeId,self.ID_Help))
return iRet
def PrintMain():
# 觸發按鈕事件,id=self. btnPrint.GetId()
#相當於執行了btnPrint所綁定的事件OnPrint ()事件。
iRet = wx.PostEvent(self,wx.CommandEvent(wx.EVT_BUTTON.typeId,self.btnPrint .GetId()))
return iRet
def PrintMain():
# 觸發checkbox事件,id=self. cbxShowDetail.GetId()
#相當於執行了cbxShowDetail所綁定的事件OnShowDetail()事件。
self.cbxShowDetail.SetValue(True)
iRet = wx.PostEvent(self,wx.CommandEvent(wx. EVT_CHECKBOX .typeId,self.cbxShowDetail .GetId() ))
return iRet
解釋:
wx.PostEvent(self ,wx.CommandEvent(wx.EVT_CHECKBOX.typeId,self.cbxShowDetail.GetId()))
參數1 ,self代表處理postevent的窗口句柄。
參數2 ,event = wx.CommandEvent( eventtype, eventid)
eventtype 分別為wx.evt_menu ,wx.evt_button, wx.evt_checkboxx。
eventid 為事件的所綁定的控件id
以上。