使用after。prog()函數是在mainloop()之后調用的,因此一旦關閉窗口,while循環就會啟動。 import tkinter as tkglobal choicechoice=Falsedef ja(): choice == True print("1 works") # To check if this worksdef nee(): choice == False print("2 works") # Also to check if worksdef prog(): if choice: print("3 works") # Program here elif not choice: print("4 works") # Program here root.after(20,prog) root = tk.Tk()root.title("Bot")label1 = tk.Label(root, text="continue?", width=75, height=25)Yes = tk.Button(root, text="Yes", width=25, height=5, command=ja)No = tk.Button(root, text="No", width=25, height=5, command=nee)Stop= tk.Button(roo