Reputation: 11
I have a script that creates a tray icon from where I call two different windows.
The tray icon library I'm using (infi.systray) creates a quit option automatically but if the windows are not closed they remain after quitting. They become non responsive and the app doesn't quit. What should I do to make the quit option destroy any window that is open?
This is my code:
```python
import time
import threading
from infi.systray import SysTrayIcon
from tkinter import *
from tkinter import ttk
makemestop=False
# functions that will be trigered by infi.systray need the variable that contains the SysTrayIcon object as a parameter
def callwin1(icon):
win1()
def callwin2(icon):
win2()
def win1():
def win1bye():
window1.destroy()
window1=Tk()
window1.title("Hi bruh this is window 1")
window1.geometry("350x30+600+600")
buttonwin1=ttk.Button(master=window1, text="Okay", command=win1bye)
buttonwin1.pack()
window1.mainloop()
def win2():
def win2bye():
window2.destroy()
window2=Tk()
window2.title("Sup money this is window 2")
window2.geometry("350x30+0+0")
buttonwin2=ttk.Button(master=window2, text="Okay", command=win2bye)
buttonwin2.pack()
window2.mainloop()
# the function to quit the program
def make_me_stop(icon):
global makemestop
makemestop=True
my_x.join()
print("I STOPPED!")
# This must go into another thread
def myloop():
try:
#while makemestop==False:
while True:
print("I'm doing something in the background")
time.sleep(3)
if makemestop==True:
break
except KeyboardInterrupt:
pass
# this is the main thread
if __name__=="__main__":
my_x=threading.Thread(target=myloop)
my_x.start()
menu_options=(
("Options", None, callwin1),
("About", None, callwin2),
)
icon=SysTrayIcon("16.ico", "My icon title", menu_options, on_quit=make_me_stop)
icon.start()
```
Upvotes: 1
Views: 117