closurestkinter窗口?
如何结束Tkinter程序? 比方说,我有这个代码:
from Tkinter import * def quit(): # code to exit root = Tk() Button(root, text="Quit", command=quit).pack() root.mainloop()  我应该如何定义quitfunction? 
 root.quit() 
 上面的行只是绕过 root.mainloop() ,如果执行quit()命令, root.mainloop()仍然会在后台运行。 
 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 
 def quit() global root root.quit() 
要么
 def quit() global root root.destroy() 
 import Tkinter as tk def quit(root): root.destroy() root = tk.Tk() tk.Button(root, text="Quit", command=lambda root=root:quit(root)).pack() root.mainloop() 
我想你错误地理解了Tkinter的quit函数,这个函数不需要你定义。
首先,你应该修改你的function如下:
 from Tkinter import * root = Tk() Button(root, text="Quit", command=root.quit).pack() root.mainloop() 
然后,你应该使用'.pyw'后缀来保存这个文件,然后双击'.pyw'文件来运行你的GUI,这次你可以通过点击Botton来结束GUI,你也可以find将不会有令人不愉快的DOS窗口(如果运行'.py'文件,quit函数将失败。
这是我第一次在这里回答问题,而我的英文真的很差! 我希望你能理解它! 非常感谢你!!!
通常的退出Python程序的方法:
 sys.exit() 
(你也可以通过退出状态)或者
 raise SystemExit 
将在Tkinter程序中正常工作。
在混乱的情况下照明…
 def quit(self): self.destroy() exit() 
一)摧毁()停止主循环和杀死窗口,但离开python运行
B)exit()停止整个过程
只是为了澄清,如果有人错过了什么摧毁()正在做,OP也问如何“结束”一个tkinter程序。
 在idlelib.PyShell模块中, Tktypes的rootvariables被定义为全局variables 
 在PyShell.main()函数结束时,它会调用root.mainloop()函数,它是一个无限循环,直到循环被root.quit()函数中断root.quit() 。 因此, root.quit()只会中断主mainloop的执行 
 为了销毁与该idlelib窗口有关的所有小部件,需要调用root.destroy() ,这是idlelib.PyShell.main()函数的最后一行。 
您可以使用:
 root.destroy() 
要么
 root.quit() 
如果这不起作用,请将root改为您的程序开始时的variables
 import tkinter main = Tk() main.destroy() main.mainloop 
使用root.destroy()! 为我工作就好了。
尝试这个:
 from Tkinter import * import sys def exitApp(): sys.exit() root = Tk() Button(root, text="Quit", command=exitApp).pack() root.mainloop() 
尝试这个。
  self.parent.destroy() self.parent.quit() 
也许你发送根参数到一个你所做的框架。 所以,如果你想完成它,你必须打电话给你的父亲,以便他可以closures它,而不是closures他的每一个孩子。