0

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()
George Wright
  • 61
  • 1
  • 10
Pan
  • 3
  • 3
  • It looks like wrong option value for `doubleclick=show()`, it is same as `doubleclick=None`, maybe `doubleclick=show`. – Jason Yang Dec 20 '21 at 07:06
  • double click was an example, there is no such option :(( – Pan Dec 20 '21 at 19:38
  • Which OS are you using? I think the possibility or not to implement the doubleclick action depends on the framework used to display the system tray. For instance, as far as I know this cannot be implemented with the Gtk AppIndicator3 backend (used in Linux by pystray) – j_4321 Dec 21 '21 at 13:45
  • What GUI library are you using for the rest of your program? Does it have to be multiplatform? – j_4321 Dec 21 '21 at 13:46
  • I am using Windows and Tkinter for the GUI, and in other programs i can open it with doubleclick in the stray icon, but in python I can't replicate it :P – Pan Dec 30 '21 at 18:49
  • You can set to execute one of the menu item action (like `show()`) when the tray icon is *clicked* (not double-click). Refer to official document on `default` option of [MenuItem](https://pystray.readthedocs.io/en/latest/reference.html#pystray.Menu). – acw1668 Dec 28 '22 at 04:50

1 Answers1

0

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()
Michael Currie
  • 13,721
  • 9
  • 42
  • 58