Pystray - How can I open the program with double click on the System Tray icon using Tkinter?

1k Views Asked by At

I need the program to be opened by double clicking on the tray icon.

Like this:

def show(icon, item):
    icon.stop()
    root.after(0, root.deiconify())
    
icon = pystray.Icon("name", image, "Title", menu)#, doubleclick=show())
icon.run()
1

There are 1 best solutions below

0
On

Unfortunately according to its own docs, pystray cannot listen for click or double-click events on the icon itself, presumably because there is no system-independent way to do it. This is also a limitation of the other system tray Python library, infi.systray.

Your example is not runnable, so I expanded on it. This snippet can run, and uses the standard device-independent drop-down menu method to start your tkinter app:

enter image description here

import tkinter as tk
import pystray
from PIL import Image

APP_ICON_FILEPATH = "pan.ico"

root = tk.Tk()
root.title("Pan's Labyrinth")


def show_app(icon, item):
    print("Stop the icon")
    icon.stop()

    print("Start the program")
    root.after(0, root.mainloop())


menu_options = [pystray.MenuItem("show_app", show_app)]

with Image.open(APP_ICON_FILEPATH) as icon_image:
    this_icon = pystray.Icon("name", icon_image, "Title", menu_options)

this_icon.run()