右键单击系统任务栏图标时,以下代码崩溃。它不会产生任何错误(除了我无法理解的复杂错误代码),因此我很难找到问题所在。
import wx
import wx.adv
class TaskbarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskbarIcon, self).__init__()
self.SetIcon(wx.Icon(wx.IconLocation("fav.ico")), "TestApp")
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnRunNow)
def CreatePopupMenu(self):
menu = wx.Menu()
run_now_item = menu.Append(-1, "Run now...", "Run now.")
menu.AppendSeparator()
exit_item = menu.Append(wx.ID_EXIT)
self.Bind(wx.EVT_MENU, self.RunNow, run_now_item)
self.Bind(wx.EVT_MENU, self.OnExit, exit_item)
return menu
def OnExit(self, event):
wx.CallAfter(self.Destroy)
self.frame.Close()
def OnRunNow(self, event):
self.ShowBalloon("Test", "Content", 5000)
class App(wx.App):
def OnInit(self):
frame = wx.Frame(None)
self.SetTopWindow(frame)
TaskbarIcon(frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
答案 0 :(得分:2)
我认为您的问题在于如何绑定事件。试试这个
import wx.adv
import wx
# Note NewId has been deprecated since wx version 4.0.1 and replaced by wx.NewIdRef
ID_RUN = wx.NewIdRef()
ID_EXIT = wx.NewIdRef()
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskBarIcon, self).__init__()
self.SetIcon(wx.Icon(wx.IconLocation("fav.ico")), "TestApp")
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
menu.Append(ID_RUN, "Run now...")
menu.AppendSeparator()
menu.Append(ID_EXIT, "Exit")
menu.Bind(wx.EVT_MENU, self.OnRunNow, id=ID_RUN)
menu.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
return menu
def on_left_down(self, event):
print ('Tray icon was left-clicked.')
def OnRunNow(self, event):
#Note: ShowBalloon is only available under Windows OS
#self.ShowBalloon("Test", "Content", 5000)
wx.MessageBox("Test", "Content")
def OnExit(self, event):
wx.CallAfter(self.Destroy)
self.frame.Close()
class App(wx.App):
def OnInit(self):
frame=wx.Frame(None)
self.SetTopWindow(frame)
TaskBarIcon(frame)
return True
def main():
app = App(False)
app.MainLoop()
if __name__ == '__main__':
main()
答案 1 :(得分:0)
晴朗的奇迪的回答使我想到了实际的问题。 RunNow
与OnRunNow
的命名不一致。解决此问题。