如何关闭tkinter窗口?

时间:2021-03-31 07:24:19

How do I end a Tkinter program? Let's say I have this code:

如何结束Tkinter程序?假设我有这个代码:

from Tkinter import *

def quit():
    # code to exit

root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()

How should I define the quit function to exit my application?

如何定义退出函数以退出应用程序?

13 个解决方案

#1


53  

You should use destroy() to close a tkinter window.

您应该使用destroy()来关闭tkinter窗口。

from Tkinter import *

root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()

Explanation:

解释:

root.quit()

The above line just Bypasses the root.mainloop() i.e root.mainloop() will still be running in background if quit() command is executed.

上面的行绕过了root.mainloop() i。如果执行了quit()命令,e root.mainloop()仍然会在后台运行。

root.destroy()

While destroy() command vanish out root.mainloop() i.e root.mainloop() stops.

当destroy()命令消失时。mainloop() i。e root.mainloop()停止。

So as you just want to quit the program so you should use root.destroy() as it will it stop the mainloop().

因此,当您只想退出程序时,您应该使用root.destroy(),因为它会停止mainloop()。

But if you want to run some infinite loop and you don't want to destroy your Tk window and want to execute some code after root.mainloop() line then you should use root.quit(). Ex:

但是,如果您想运行某个无限循环,又不想破坏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

#2


34  

def quit()
    global root
    root.quit()

or

def quit()
    global root
    root.destroy()

#3


13  

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()

#4


6  

I think you wrongly understood the quit function of Tkinter. This function does not require you to define.

我想你误解了Tkinter的退出功能。这个函数不需要定义。

First, you should modify your function as follows:

首先,你应该修改你的功能如下:

from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.quit).pack()
root.mainloop()

Then, you should use '.pyw' suffix to save this files and double-click the '.pyw' file to run your GUI, this time, you can end the GUI with a click of the Button, and you can also find that there will be no unpleasant DOS window. (If you run the '.py' file, the quit function will fail.)

那么,你应该用'。pyw后缀来保存文件并双击。pyw的文件来运行您的GUI,这次,您可以用单击按钮来结束GUI,您还可以发现没有不愉快的DOS窗口。(如果你跑。py'文件,退出功能将失败)

#5


4  

The usual method to exit a Python program:

通常退出Python程序的方法是:

sys.exit()

(to which you can also pass an exit status) or

(您也可以向其传递退出状态)或

raise SystemExit

will work fine in a Tkinter program.

将在Tkinter程序中正常工作。

#6


3  

Illumination in case of confusion...

在混乱的情况下照明…

def quit(self):
    self.destroy()
    exit()

A) destroy() stops the mainloop and kills the window, but leaves python running

A) destroy()停止主循环并杀死窗口,但是让python运行。

B) exit() stops the whole process

B) exit()停止整个过程

Just to clarify in case someone missed what destroy() was doing, and the OP also asked how to "end" a tkinter program.

只是为了澄清万一有人错过了destroy()在做什么,OP也问了如何“结束”tkinter程序。

#7


1  

In idlelib.PyShell module, root variable of type Tk is defined to be global

在idlelib。PyShell模块,Tk类型的根变量被定义为全局变量

At the end of PyShell.main() function it calls root.mainloop() function which is an infinite loop and it runs till the loop is interrupted by root.quit() function. Hence, root.quit() will only interrupt the execution of mainloop

在PyShell.main()函数的末尾,它调用root.mainloop()函数,这是一个无限循环,它一直运行到循环被root.quit()函数中断。因此,root.quit()只会中断mainloop的执行

In order to destroy all widgets pertaining to that idlelib window, root.destroy() needs to be called, which is the last line of idlelib.PyShell.main() function.

为了销毁与idlelib窗口相关的所有小部件,需要调用root.destroy(),这是idleli . pyshell .main()函数的最后一行。

#8


1  

You can use:

您可以使用:

root.destroy()

Or

root.quit()

If that does not work, change root to what ever your variable was at the start of your program

如果这不起作用,请将根值改为程序开始时的变量值

import tkinter

main = Tk()

main.destroy()

main.mainloop

#9


0  

def quit1():
     root.destroy()

Button(root, text="Quit", command=quit1).pack()
root.mainloop()

#10


0  

import sys
from Tkinter import *
def quit():
    sys.exit()
root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()

Should do what you are asking.

应该按你的要求去做。

#11


-1  

Use root.destroy()! Works for me just fine.

使用root.destroy()!对我来说很好。

#12


-2  

Try this:

试试这个:

from Tkinter import *
import sys
def exitApp():
    sys.exit()
root = Tk()
Button(root, text="Quit", command=exitApp).pack()
root.mainloop()

#13


-3  

try this.

试试这个。

    self.parent.destroy() 
    self.parent.quit()

maybe you send root like parameter to a frame that you did. so If you want to finish it you have to call your father so he can close it all, instead of closing each one of his children.

也许你会把根类参数发送给你做过的一个框架。所以如果你想完成它,你必须打电话给你的父亲,这样他就可以把它全部关闭,而不是关闭他的每个孩子。

#1


53  

You should use destroy() to close a tkinter window.

您应该使用destroy()来关闭tkinter窗口。

from Tkinter import *

root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()

Explanation:

解释:

root.quit()

The above line just Bypasses the root.mainloop() i.e root.mainloop() will still be running in background if quit() command is executed.

上面的行绕过了root.mainloop() i。如果执行了quit()命令,e root.mainloop()仍然会在后台运行。

root.destroy()

While destroy() command vanish out root.mainloop() i.e root.mainloop() stops.

当destroy()命令消失时。mainloop() i。e root.mainloop()停止。

So as you just want to quit the program so you should use root.destroy() as it will it stop the mainloop().

因此,当您只想退出程序时,您应该使用root.destroy(),因为它会停止mainloop()。

But if you want to run some infinite loop and you don't want to destroy your Tk window and want to execute some code after root.mainloop() line then you should use root.quit(). Ex:

但是,如果您想运行某个无限循环,又不想破坏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

#2


34  

def quit()
    global root
    root.quit()

or

def quit()
    global root
    root.destroy()

#3


13  

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()

#4


6  

I think you wrongly understood the quit function of Tkinter. This function does not require you to define.

我想你误解了Tkinter的退出功能。这个函数不需要定义。

First, you should modify your function as follows:

首先,你应该修改你的功能如下:

from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.quit).pack()
root.mainloop()

Then, you should use '.pyw' suffix to save this files and double-click the '.pyw' file to run your GUI, this time, you can end the GUI with a click of the Button, and you can also find that there will be no unpleasant DOS window. (If you run the '.py' file, the quit function will fail.)

那么,你应该用'。pyw后缀来保存文件并双击。pyw的文件来运行您的GUI,这次,您可以用单击按钮来结束GUI,您还可以发现没有不愉快的DOS窗口。(如果你跑。py'文件,退出功能将失败)

#5


4  

The usual method to exit a Python program:

通常退出Python程序的方法是:

sys.exit()

(to which you can also pass an exit status) or

(您也可以向其传递退出状态)或

raise SystemExit

will work fine in a Tkinter program.

将在Tkinter程序中正常工作。

#6


3  

Illumination in case of confusion...

在混乱的情况下照明…

def quit(self):
    self.destroy()
    exit()

A) destroy() stops the mainloop and kills the window, but leaves python running

A) destroy()停止主循环并杀死窗口,但是让python运行。

B) exit() stops the whole process

B) exit()停止整个过程

Just to clarify in case someone missed what destroy() was doing, and the OP also asked how to "end" a tkinter program.

只是为了澄清万一有人错过了destroy()在做什么,OP也问了如何“结束”tkinter程序。

#7


1  

In idlelib.PyShell module, root variable of type Tk is defined to be global

在idlelib。PyShell模块,Tk类型的根变量被定义为全局变量

At the end of PyShell.main() function it calls root.mainloop() function which is an infinite loop and it runs till the loop is interrupted by root.quit() function. Hence, root.quit() will only interrupt the execution of mainloop

在PyShell.main()函数的末尾,它调用root.mainloop()函数,这是一个无限循环,它一直运行到循环被root.quit()函数中断。因此,root.quit()只会中断mainloop的执行

In order to destroy all widgets pertaining to that idlelib window, root.destroy() needs to be called, which is the last line of idlelib.PyShell.main() function.

为了销毁与idlelib窗口相关的所有小部件,需要调用root.destroy(),这是idleli . pyshell .main()函数的最后一行。

#8


1  

You can use:

您可以使用:

root.destroy()

Or

root.quit()

If that does not work, change root to what ever your variable was at the start of your program

如果这不起作用,请将根值改为程序开始时的变量值

import tkinter

main = Tk()

main.destroy()

main.mainloop

#9


0  

def quit1():
     root.destroy()

Button(root, text="Quit", command=quit1).pack()
root.mainloop()

#10


0  

import sys
from Tkinter import *
def quit():
    sys.exit()
root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()

Should do what you are asking.

应该按你的要求去做。

#11


-1  

Use root.destroy()! Works for me just fine.

使用root.destroy()!对我来说很好。

#12


-2  

Try this:

试试这个:

from Tkinter import *
import sys
def exitApp():
    sys.exit()
root = Tk()
Button(root, text="Quit", command=exitApp).pack()
root.mainloop()

#13


-3  

try this.

试试这个。

    self.parent.destroy() 
    self.parent.quit()

maybe you send root like parameter to a frame that you did. so If you want to finish it you have to call your father so he can close it all, instead of closing each one of his children.

也许你会把根类参数发送给你做过的一个框架。所以如果你想完成它,你必须打电话给你的父亲,这样他就可以把它全部关闭,而不是关闭他的每个孩子。