如何结束Tkinter程序?假设我有以下代码:from Tkinter import *def quit(): # code to exitroot = Tk()Button(root, text="Quit", command=quit).pack()root.mainloop()如何定义quit退出应用程序的功能?
3 回答
![?](http://img1.sycdn.imooc.com/5458631e0001ffd402200220-100-100.jpg)
largeQ
TA贡献2039条经验 获得超7个赞
您应该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
添加回答
举报
0/150
提交
取消