moses-palmer / pystray

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

run_detached appears to do nothing in Linux #123

Closed liambrownweb closed 2 years ago

liambrownweb commented 2 years ago

OS: Pop!OS 21.10 Desktop: Gnome 40.4

#!/usr/bin/python3
import os
import pystray
from PIL import Image
import tkinter as tk

def handle_exit():
    quit();

def handle_show():
    print("Quack!")

def tray_icon():
    icon = pystray.Icon(
    'Rubber duck',
    menu=tray_menu(),
    icon=Image.open(os.getcwd()+'/tray.png'))
    return icon

def tray_menu():
    return pystray.Menu(
            pystray.MenuItem(
                'Show me the ducky!!!',
                handle_show,
                default=True),
            pystray.MenuItem(
                'Exit',
                handle_exit))

tray = tray_icon()
tray.run_detached()

root = tk.Tk()
root.overrideredirect(True)
root.config(bg="blue", bd=0, highlightthickness=0)
root.attributes("-alpha", "100")

canvas = tk.Canvas(root, bg="blue", bd=0, highlightthickness=0)
canvas.pack()

tk_img = tk.PhotoImage(file="duck.png")
canvas.create_image(0, 0, image=tk_img, anchor="nw")

root.mainloop()

The tray icon simply never appears. Tried replacing the version of pystray on PyPI with the latest pull from here (which claimed to implement run_detached) and same result, nothing appeared. Using the run method instead does work.

moses-palmer commented 2 years ago

Thank you for your report.

run_detached requires that the framework you are integrating with uses the same kind of mainloop as this library, and TKinter uses an X mainloop, whereas the usable backends for this library use GTK.

If you do not target macOS, my suggestion would be to run the icon in a separate thread and simply use run instead.

I will update the documentation to make this more clear.