Python 如何关闭tkinter窗口?

Python 如何关闭tkinter窗口?,python,tkinter,Python,Tkinter,如何结束Tkinter程序?假设我有以下代码: from Tkinter import * def quit(): # code to exit root = Tk() Button(root, text="Quit", command=quit).pack() root.mainloop() 如何定义退出应用程序的quit函数 def quit() root.quit() 或 退出Python程序的常用方法: sys.exit() (您也可以向其传递退出状态)或 将在

如何结束Tkinter程序?假设我有以下代码:

from Tkinter import *

def quit():
    # code to exit

root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()
如何定义退出应用程序的
quit
函数

def quit()
    root.quit()


退出Python程序的常用方法:

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


将在Tkinter程序中正常工作。

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

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()
from Tkinter import *

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

说明:

root.quit()
上面的一行只是绕过
root.mainloop()
root.mainloop()
如果执行
quit()
命令,则仍将在后台运行

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

我认为你错误地理解了Tkinter的退出功能。此函数不需要您定义

首先,您应该按如下方式修改您的函数:

from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.quit).pack()
root.mainloop()
然后,您应该使用“.pyw”后缀来保存此文件,然后双击“.pyw”文件来运行GUI,这一次,您可以通过单击按钮来结束GUI,并且您还可以发现不会出现令人不快的DOS窗口。(如果运行“.py”文件,退出功能将失败。)

试试这个

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

也许您将类似根的参数发送到您所做的帧。因此,如果你想完成它,你必须打电话给你父亲,让他可以关闭它,而不是关闭他的每个孩子

在混乱情况下的照明

def quit(self):
    self.destroy()
    exit()
A) destroy()停止mainloop并终止窗口,但保持python运行

B) exit()停止整个过程

为了澄清是否有人错过了destroy()正在做的事情,OP还询问了如何“结束”tkinter程序。

尝试以下方法:

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()
from Tkinter import *
import sys
def exitApp():
    sys.exit()
root = Tk()
Button(root, text="Quit", command=exitApp).pack()
root.mainloop()

idlelib.PyShell
模块中,
root
类型为
Tk
的变量被定义为全局变量

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

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

您可以使用:

root.destroy()

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

import tkinter

main = Tk()

main.destroy()

main.mainloop

应该按你的要求去做。

最简单的方法是单击红色按钮(在macOS上最左边,在Windows上最右边)。 如果要将特定功能绑定到按钮小部件,可以执行以下操作:

class App:
    def __init__(self, master)
        frame = Tkinter.Frame(master)
        frame.pack()
        self.quit_button = Tkinter.Button(frame, text = 'Quit', command = frame.quit)
        self.quit_button.pack()
或者,为了使事情更复杂一些,使用协议处理程序和
destroy()
方法

import tkMessageBox

def confirmExit():
    if tkMessageBox.askokcancel('Quit', 'Are you sure you want to exit?'):
        root.destroy()
root = Tk()
root.protocol('WM_DELETE_WINDOW', confirmExit)
root.mainloop()

如果有人想将其退出按钮绑定到关闭整个GUI:

master = Tk()
master.title("Python")

def close(event):
    sys.exit()

master.bind('<Escape>',close)
master.mainloop()
master=Tk()
硕士头衔(“Python”)
def关闭(事件):
sys.exit()
主绑定(“”,关闭)
master.mainloop()
对于菜单栏:

def quit():
    root.destroy()

menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="menubarname", menu=filemenu)
root.config(menu=menubar)
root.mainloop()

您只需键入以下内容:

root.destroy()

您甚至不需要quit()函数,因为当您将其设置为commmand时,它将退出整个程序。

我使用以下代码退出Tkinter窗口:

from tkinter import*
root=Tk()
root.bind("<Escape>",lambda q:root.destroy())
root.mainloop()


下面是代码片段。我提供了一个小场景

import tkinter as tk
from tkinter import *

root = Tk()

def exit():
    if askokcancel("Quit", "Do you really want to quit?"):
        root.destroy()

menubar = Menu(root, background='#000099', foreground='white',
               activebackground='#004c99', activeforeground='white')

fileMenu = Menu(menubar,  tearoff=0, background="grey", foreground='black',
                activebackground='#004c99', activeforeground='white')
menubar.add_cascade(label='File', menu=fileMenu)

fileMenu.add_command(label='Exit', command=exit)

root.config(bg='#2A2C2B',menu=menubar)

if __name__ == '__main__':
    root.mainloop()
我在这里创建了一个空白窗口&在同一窗口(根窗口)上添加文件菜单选项,在这里我只添加一个选项退出

然后简单地根运行mainloop


试着在根目录下执行一次。销毁将起作用。
root.quit
也会起作用

就我而言,我有

quitButton = Button(frame, text = "Quit", command = root.destroy)
希望能有帮助

raise SystemExit
第一次尝试就成功了, 在哪里


没有

有一个简单的单行答案:

在命令中写入-
exit()


就这样

当然,您可以按如下方式将命令分配给按钮,但是,如果您正在制作UI,建议将相同的命令分配给“X”按钮:

from tkinter import *

def quit(root):
    root.close()

root = Tk()
root.title("Quit Window")

def quit(root):
    root.close()

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

root.mainloop()
试试这个

Button(root, text="Quit", command=lambda:root.destroy()).pack()

有人知道哪种方法更“正确”吗?或者这是一种比另一种更易读的方法吗?对不起,我找到了答案,我想和大家分享一下。最好使用
root.destroy()
,因为它会终止主程序循环。请参阅:如果要关闭打开的winodw,请使用destroy()方法。如果您想关闭所有use quit(),请不要发布推测性答案。理想情况下,只有当你有一个经过验证的答案时,你才应该发帖。如果您有建议,这些建议可以放在注释中。当您使用类时,self.parent.destroy()在我的情况下起作用。如果使用root.quit(),以后如何在另一个要销毁的脚本中再次找到该窗口(以便不继续使用系统资源)?您的第一个语句为false。调用
quit
将销毁所有小部件;如果小部件被销毁,
mainloop
将退出。经过一些研究,我相信这也适用于所有正在执行的代码。因此,如果命令行脚本中有TKinter的mainloop(),请使用root.quit()而不是root.destroy(),否则命令行脚本将无法在mainloop()之后继续执行代码。我测试了这个,它对我有效(我知道TKinter不打算用于这样的设计,但是,它有效)多次破坏TKinter窗口并多次启动是一个好的解决方案吗?我将调用许多窗口(同名),那么在这种情况下该如何工作呢?嘿,Harrison,你有一个很晚才回答的问题
from tkinter import*
root=Tk()
Button(root,text="quit",command=quit).pack()
root.mainloop()
from tkinter import*
root=Tk()
Button(root,text="exit",command=exit).pack()
root.mainloop()
import tkinter as tk
from tkinter import *

root = Tk()

def exit():
    if askokcancel("Quit", "Do you really want to quit?"):
        root.destroy()

menubar = Menu(root, background='#000099', foreground='white',
               activebackground='#004c99', activeforeground='white')

fileMenu = Menu(menubar,  tearoff=0, background="grey", foreground='black',
                activebackground='#004c99', activeforeground='white')
menubar.add_cascade(label='File', menu=fileMenu)

fileMenu.add_command(label='Exit', command=exit)

root.config(bg='#2A2C2B',menu=menubar)

if __name__ == '__main__':
    root.mainloop()
quitButton = Button(frame, text = "Quit", command = root.destroy)
raise SystemExit
self.destroy()
root.destroy()
from tkinter import *

def quit(root):
    root.close()

root = Tk()
root.title("Quit Window")

def quit(root):
    root.close()

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

root.mainloop()
def quit(self): # Your exit routine
   self.root.destroy()

self.root.protocol("WM_DELETE_WINDOW", self.quit) # Sets the command for the "X" button 

Button(text="Quit", command=self.quit) # No ()

Button(root, text="Quit", command=lambda:root.destroy()).pack()