Pystray | Unable to click on a script tray icon to exit the script in Python

33 Views Asked by At

I am making a mouse jiggler with pyautogui and pystray. I cannot right click on the tray icon while the script is executed.

I guess, this happens because of 'while true' condition, but I don't know how to avoid it. So I am asking for some help. Do I need using threads maybe?

import pyautogui
import time 
import pystray
import PIL.Image

image = PIL.Image.open("macos.jpg")

def clicked(icon, item):
    if str(item) == "Run":
        while True:
            pyautogui.moveRel(1, 0, duration = 0)
            pyautogui.moveRel(-1, 0, duration = 0)
            time.sleep(10)
    elif str(item) == "Exit":
        icon.stop()

icon = pystray.Icon("macos", image, menu=pystray.Menu(
    pystray.MenuItem("Run", clicked),
    pystray.MenuItem("Exit", clicked)
))

icon.run()

0

There are 0 best solutions below