0

I'm having some trouble getting this code Link running on Python3 with WX. I made some edits to the code but I'm still getting this error. What am I missing? This is Python 3.6 with the Project Phoenix WX module on Windows7. Pretty frustrated, since I've tried about 4 different test code samples to implement a systray Icon and none of them work.

import wx.adv

TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'icon.png'

def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.AppendItem(item)
return item

class TaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self, frame):
        self.frame = frame
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)

def CreatePopupMenu(self):
    menu = wx.Menu()
    create_menu_item(menu, 'Say Hello', self.on_hello)
    menu.AppendSeparator()
    create_menu_item(menu, 'Exit', self.on_exit)
    return menu

def set_icon(self, path):
    icon = wx.Icon(wx.Bitmap(path))
    self.SetIcon(icon, TRAY_TOOLTIP)

def on_left_down(self, event):
    print ('Tray icon was left-clicked.')

def on_hello(self, event):
    print ('Hello, world!')

def on_exit(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()

enter image description here

user2059972
  • 145
  • 2
  • 11

2 Answers2

3

I'm still using wxpython Classic, so I can't test this.
wx.EVT_TASKBAR_LEFT_DOWN is the normal event but you are importing TaskBarIcon from wx.adv, so I assume that you should use wx.adv.EVT_TASKBAR_LEFT_DOWN

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Gah, that was it! I swear I had tried that, must of had something else messed up at the time aswell. Also, for future reference, "AppendItem" needs to change to "Append". Thanks for the help! – user2059972 Sep 27 '17 at 01:51
0

Working Code, Python 3.9

import wx.adv

TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'off.png'

def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.AppendItem(item)
    return item

class TaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self, frame):
        self.frame = frame

        super(TaskBarIcon, self).__init__()
        icon = wx.Icon(wx.Bitmap(TRAY_ICON, wx.BITMAP_TYPE_ANY))

        self.SetIcon(icon,TRAY_TOOLTIP)
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)

    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Say Hello', self.on_hello)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu

    def set_icon(self, path):
        icon = wx.Icon(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)

    def on_left_down(self, event):
        print ('Tray icon was left-clicked.')

    def on_hello(self, event):
        print ('Hello, world!')

    def on_exit(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()
blackmoon
  • 324
  • 6
  • 17