moses-palmer / pystray

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

Is there a way to change a name of a MenuItem after clicked on? System tray Windows 10 #157

Closed LawMixer closed 9 months ago

LawMixer commented 9 months ago

Wanting to do a timer sort of application for fun, and "Start" is going to be the main thing and then whenever it gets pressed, it gets changed to "Stop"

I'm not sure how to do it or if there's any documentation on it, any help would be appreciated!

moses-palmer commented 9 months ago

Have a look at the documentation. In your case, setting the text to a lambda reflecting the state of your application instead of a static string would be the way to go.

LawMixer commented 9 months ago

I've tried what was written in the documentation, and it does not refresh the value in the system tray. Maybe, I did something wrong and I need a second set of eyes to view my errors, any help would be appreciated, thanks!

import pystray
from PIL import Image

image = Image.open("5968322.png")

state = "Start"

def after_click(icon2: pystray.Icon, query):    
    global state 

    if str(query) == "Start":
      print("Starting..")
    elif str(query) == "Stop":
      print("Stopping..")
    elif str(query) == "Quit":
      icon.stop()

    if state == "Start":
      state = "Stop"

    print(state)
    icon.menu = False
    icon.menu = menu

menu = pystray.Menu(
  pystray.MenuItem("Start", action=after_click, checked=lambda item: state), 
  pystray.MenuItem("Quit", action=after_click)
)
icon = pystray.Icon("T", image, "Testing", menu=menu)

icon.run()