需要注意的是,这里是托盘上的图标弹出菜单是覆盖了CreatePopupMenu
也可以绑定2个方法,一个wx.EVT_TASKBAR_RIGHT_DOWN,方法里面生成menu,然后再来一个wx.EVT_MENU,定义要处理的事件函数。
还有一个就是Wx窗体上的最小化按钮,触发的事件是 wx.EVT_ICONIZE,而根本就没有定义什么wx.EVT_MINIMIZE,但是最大化,有个wx.EVT_MAXIMIZE。
#!/usr/bin/python # _*_ coding: utf-8 _*_ import wx class TaskBarIcon(wx.TaskBarIcon): ID_Hello = wx.NewId() def __init__(self, frame): wx.TaskBarIcon.__init__(self) self.frame = frame self.SetIcon(wx.Icon(name='wx.ico', type=wx.BITMAP_TYPE_ICO), 'TaskBarIcon!') self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick) self.Bind(wx.EVT_MENU, self.OnHello, id=self.ID_Hello) def OnTaskBarLeftDClick(self, event): if self.frame.IsIconized(): self.frame.Iconize(False) if not self.frame.IsShown(): self.frame.Show(True) self.frame.Raise() def OnHello(self, event): wx.MessageBox('Hello From TaskBarIcon!', 'Prompt') # override def CreatePopupMenu(self): menu = wx.Menu() menu.Append(self.ID_Hello, 'Hello') return menu class Frame(wx.Frame): def __init__( self, parent=None, id=wx.ID_ANY, title='TaskBarIcon', pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE ): wx.Frame.__init__(self, parent, id, title, pos, size, style) # create a welcome screen screen = wx.Image(self.screenIm).ConvertToBitmap() wx.SplashScreen(screen, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,1000, None, -1) wx.Yield() self.SetIcon(wx.Icon('wx.ico', wx.BITMAP_TYPE_ICO)) panel = wx.Panel(self, wx.ID_ANY) button = wx.Button(panel, wx.ID_ANY, 'Hide Frame', pos=(60, 60)) sizer = wx.BoxSizer() sizer.Add(button, 0) panel.SetSizer(sizer) self.taskBarIcon = TaskBarIcon(self) # bind event self.Bind(wx.EVT_BUTTON, self.OnHide, button) self.Bind(wx.EVT_CLOSE, self.OnClose) self.Bind(wx.EVT_ICONIZE, self.OnIconfiy) # 最小化事件绑定 def OnHide(self, event): self.Hide() def OnIconfiy(self, event): wx.MessageBox('Frame has been iconized!', 'Prompt') event.Skip() def OnClose(self, event): self.taskBarIcon.Destroy() self.Destroy() def TestFrame(): app = wx.PySimpleApp() frame = Frame(size=(640, 480)) frame.Centre() frame.Show() app.MainLoop() if __name__ == '__main__': TestFrame()