擅长:python、mysql、java
<p><strong>您应该使用<code>destroy()</code>关闭tkinter窗口。</strong></p>
<pre><code>from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
</code></pre>
<hr/>
<p><strong>说明:</strong></p>
<pre><code>root.quit()
</code></pre>
<p>如果执行<code>quit()</code>命令,上面的行只是绕过<code>root.mainloop()</code>即<code>root.mainloop()</code>仍将在后台运行。</p>
<pre><code>root.destroy()
</code></pre>
<p>当<code>destroy()</code>命令消失时<code>root.mainloop()</code>即<code>root.mainloop()</code>停止。</p>
<p>因此,如果您只想退出程序,那么应该使用<code>root.destroy()</code>,因为它会停止<code>mainloop()</code>。</p>
<p>但是如果您想运行一些无限循环,并且不想破坏您的Tk窗口,并且想在<code>root.mainloop()</code>行之后执行一些代码,那么您应该使用<code>root.quit()</code>。例如:</p>
<pre><code>from Tkinter import *
def quit():
global root
root.quit()
root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
root.mainloop()
#do something
</code></pre>