如何結束Tkinter程序?假設我有以下代碼:from Tkinter import *def quit(): # code to exitroot = Tk()Button(root, text="Quit", command=quit).pack()root.mainloop()如何定義quit退出應用程序的功能?
3 回答

湖上湖
TA貢獻2003條經驗 獲得超2個贊
您應該destroy()用來關閉tkinter窗口。
from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
說明:
root.quit()
上面的行只是繞過了root.mainloop()ie root.mainloop(),如果quit()執行了命令,ie 仍將在后臺運行。
root.destroy()
當destroy()命令消失時,root.mainloop()即root.mainloop()停止。
因此,您只想退出該程序,就應該使用root.destroy()它,因為它會停止mainloop()。
但是如果你想運行無限循環并且你不想破壞你的Tk窗口并且想root.mainloop()在行之后執行一些代碼,那么你應該使用root.quit()。例如:
from Tkinter import *
def quit():
global root
root.quit()
root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
root.mainloop()
#do something
- 3 回答
- 0 關注
- 639 瀏覽
添加回答
舉報
0/150
提交
取消