0

我有一个脚本,它创建一个托盘图标,我从中调用两个不同的窗口。

我正在使用的托盘图标库(infi.systray)会自动创建一个退出选项,但如果窗口没有关闭,它们会在退出后保留。他们变得无响应并且应用程序不会退出。我应该怎么做才能使退出选项破坏任何打开的窗口?

这是我的代码:

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

0 回答 0