[英]wxPython TaskbarIcon Menu Crash
The following code is crashing when right-clicking on the system tray icon. 右键单击系统任务栏图标,以下代码崩溃。 It doesn't produce any error (except a complex error code that I cannot make sense of), so I'm having trouble tracking the issue down.
它不会产生任何错误(除了我无法理解的复杂错误代码),因此我很难找到问题所在。
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()
i think your problem lies with how you bind your events. 我认为您的问题在于如何绑定事件。 try this
尝试这个
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()
sunny chidi's answer lead me to the actual problem. Sunny Chidi的回答将我引向了实际问题。 There was some inconsistent naming going on with
RunNow
vs OnRunNow
. RunNow
与OnRunNow
命名不一致。 Resolving this fixed it. 解决这个问题就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.