Program in stray stops working

Hi, i made program with ttkbootstrap, made it constantly repeat itself each 10 seconds and using pystray hide it in a windows stray. But when i hide it in a tray, working process just stops, program doesn’t call itself when the window is hide. Here is all my pystray code:

def restoreFromTray(icon, item):
    root.deiconify()   **#root = tb.Window()**
    icon.stop()
    
def close(icon, item):
    icon.stop()
    root.quit()
    
def minimizeToTray():
    root.withdraw() **#Here i hide the program**
    image = PIL.Image.open("lightIcon.ico")
    menu = (pystray.MenuItem("Show", restoreFromTray), pystray.MenuItem("Quit", close))
    icon = pystray.Icon("BRIGHTNESS PROGRAM", image, "BP", menu)
    icon.run()
    
root.protocol("WM_DELETE_WINDOW", minimizeToTray)

How can i make it work even from tray? Many thanks if you have ideas or solutions

I can guess that pystray is from PyPI, but what OS are you seeing a problem on? That I cannot guess from what you wrote.

Also please post a small, complete, program that shows the problem so others can test and comment on the code.

1 Like

I have this program:

def program():
    currentSecond = int(strftime("%H"))
    #print(currentSecond)
    if currentSecond >= firstSecond and currentSecond < secondSecond:
        set_brightness(firstChange)
    elif currentSecond >= secondSecond or currentSecond < firstSecond:
        set_brightness(secondChange)
    root.after(10000, program)
        
root.after(10000, program)

It repeats every 10 seconds, then if i close the window it doesn’t stop the program it calls “minimizeToTray” method, which is here:

def minimizeToTray():
    root.withdraw()
    image = PIL.Image.open("lightIcon.ico")
    menu = (pystray.MenuItem("Show", restoreFromTray), pystray.MenuItem("Quit", close))
    icon = pystray.Icon("BRIGHTNESS PROGRAM", image, "BP", menu)
    icon.run()

This code hides my window and adds icon in tray, that have two options “close” and “show”
The whole program works great, but if i call this method(minimizeToTray), original 10 seconds stop and nothing changes till i show it again

If it’ll help, here is the whole code:

from ensurepip import bootstrap
from struct import pack
import threading
from turtle import left
from screen_brightness_control import get_brightness, set_brightness, fade_brightness
from time import sleep, strftime
from tkinter import *
from ttkbootstrap.constants import *            #LIBRARIES
import ttkbootstrap as tb
import pystray
import PIL.Image

firstSecond = int()
firstChange = int()
secondSecond = int()                            #VARIABLES
secondChange = int()

root = tb.Window(themename="superhero")
root.title("BRIGHTNESS PROGRAM")                  #TKINTER WINDOW
root.geometry("850x550")
root.iconbitmap("lightIcon.ico")

try:
    savedData = open("savedData.txt", "r")
    content = savedData.read()
    savedData.close()
    content = content.split()
    print("SAVEDDATA FILE FOUND")
except:
    savedData = open("savedData.txt", "w")
    content = savedData.write("")
    savedData.close()
    print("CREATED SAVEDDATA FILE")
    
    savedData = open("savedData.txt", "r")
    content = savedData.read()
    savedData.close()
    content = content.split()

try:
    firstSecond = int(content[0])
    firstChange = int(content[1])
    secondSecond = int(content[2])
    secondChange = int(content[3])
except:
    print("There are no prepared variables")

def program():
    currentSecond = int(strftime("%H"))
    #print(currentSecond)                              #THE MAIN PROGRAM
    if currentSecond >= firstSecond and currentSecond < secondSecond:
        set_brightness(firstChange)
    elif currentSecond >= secondSecond or currentSecond < firstSecond:
        set_brightness(secondChange)
    root.after(10000, program)
        
root.after(10000, program)                        #REPEAT IT EVERY 10 SECONDS

def save():
    try:
        global firstSecond
        firstSecond = int(fsEntry.get())
        global secondSecond
        secondSecond = int(ssEntry.get())
        global firstChange
        firstChange = int(ffEntry.get())
        global secondChange
        secondChange = int(sfEntry.get())
        savedData = open("savedData.txt", "w")
        savedData.write(f"{firstSecond}\n{firstChange}\n{secondSecond}\n{secondChange}")
        savedData.close()
    except:
        print("LOL ERROR")
        
def restoreFromTray(icon, item):
    root.deiconify()
    icon.stop()
    
def close(icon, item):
    icon.stop()
    root.quit()
    
def minimizeToTray():                #HIDES THE WINDOW AND ADDS TRAY ICON
    root.withdraw()
    image = PIL.Image.open("lightIcon.ico")
    menu = (pystray.MenuItem("Show", restoreFromTray), pystray.MenuItem("Quit", close))
    icon = pystray.Icon("BRIGHTNESS PROGRAM", image, "BP", menu)
    icon.run()
    
root.protocol("WM_DELETE_WINDOW", minimizeToTray)
        
myLabel = tb.Label(text="Brightness Program", font=("Berlin Sans FB", 35), bootstyle="default")
myLabel.pack(pady=0)

fsLabel = tb.Label(text="First time", font=("Berlin Sans FB", 10), bootstyle="default")
fsLabel.pack()

fsEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=4)
fsEntry.pack(pady=5)

ffLabel = tb.Label(text="First force", font=("Berlin Sans FB", 10), bootstyle="default")
ffLabel.pack()

ffEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=3)
ffEntry.pack(pady=5)

myLabel2 = tb.Label(text="|", font=("Berlin Sans FB", 10), bootstyle="default")
myLabel2.pack(padx=5)

ssLabel = tb.Label(text="Second time", font=("Berlin Sans FB", 10), bootstyle="default")
ssLabel.pack()

ssEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=4)
ssEntry.pack(pady=5)

sfLabel = tb.Label(text="Second force", font=("Berlin Sans FB", 10), bootstyle="default")
sfLabel.pack()

sfEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=3)
sfEntry.pack(pady=5)

smLabel = tb.Label(text="Smoothness", font=("Berlin Sans FB", 10), bootstyle="default")
smLabel.pack()

smEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 15), foreground="light blue", width=2)
smEntry.pack(pady=5)

myButton = tb.Button(text="SAVE", bootstyle="primary, outline", command=save)
myButton.pack(pady=30)

root.mainloop()

I asked you to tell us which operating system you are running this on.
Which one is it macOS, Windows, KDE plasma, Gnome etc?

1 Like

Ah, windows

I have a solution
instead of using root.after(10000, program) i’m using currently

program = threading.Thread(target=program)
program.daemon = True
program.start()

The whole code of this is here:

def program():
    while True:
        currentSecond = int(strftime("%H"))
        #print(currentSecond)
        if currentSecond >= firstSecond and currentSecond < secondSecond:
            set_brightness(firstChange)
        elif currentSecond >= secondSecond or currentSecond < firstSecond:
            set_brightness(secondChange)
        sleep(10)

program = threading.Thread(target=program)
program.daemon = True
program.start()

Now everything is working even if the window is closed
Still many thanks!

If i’ll help someone:
Above - it’s not a solution, later i noticed that there were no change, and program still didn’t work. Real solution is easy:

def minimizeToTray():
    root.withdraw()
    image = PIL.Image.open("lightIcon.ico")
    menu = (pystray.MenuItem("Show", restoreFromTray), pystray.MenuItem("Quit", close))
    icon = pystray.Icon("BRIGHTNESS PROGRAM", image, "BP", menu)
    icon.run_detached()

Here i minimize the window, and instead of icon.run() i spelled icon.run_detached(). It lets threading work properly and call all need methods