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()