moses-palmer / pystray

GNU General Public License v3.0
473 stars 59 forks source link

pystray.Menu does only work, when pystray.Icon.run() is called in main thread #69

Closed Ari24-cb24 closed 3 years ago

Ari24-cb24 commented 3 years ago

Hey there,

I wanna build a windows application, that has no window but with a systemtray so if the user clicks the tray, there should be a little menu that contains a button "quit" so the program does quit.

In the docs I've read that pystray.Icon.run() can be called in another thread.

Unfortunately, this doesn't affect the menu so if I call pystray.Icon.run() in a custom Thread, the menu doesn't appear. If I run it in the main thread, the menu does appear.

Greets

Ari24-cb24 commented 3 years ago

I did some further investigation and found out, that also if I create the stray in the custom thread, the menu still doesnt work.

My current code (dummy code):

class PyStray(threading.Thread):
    def __init__(self, stray):
        super().__init__()
        self.stray = pystray.Icon("DesktopUtils", menu=pystray.Menu(
            pystray.MenuItem(
                "checked",
                print,
                checked=lambda _: False
            )
        ))
        self.stray.icon = Image.open("./img/icon.ico")

    def run(self):
        self.stray.visible = True
        self.stray.run()

    def stop(self):
        self.stray.visible = False
        sys.exit()
moses-palmer commented 3 years ago

Thank you for your report.

As stated in the documentation (also here), run must be called from the main thread.