我怎样关闭一个窗口?

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

from Tkinter import *


def quit():
# code to exit


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

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

399680 次浏览
def quit()
root.quit()

或者

def quit()
root.destroy()

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

sys.exit()

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

raise SystemExit

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

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

from Tkinter import *
#use tkinter instead of Tkinter (small, not capital T) if it doesn't work
#as it was changed to tkinter in newer Python versions


root = Tk()
Button(root, text="Quit", command=root.destroy).pack() #button to close the window
root.mainloop()

说明:

root.quit()

上面这行只有 旁路手术root.mainloop(),也就是说,如果执行 quit()命令,root.mainloop()仍将运行 在背景中

root.destroy()

destroy()命令从 root.mainloop()消失时,也就是说,root.mainloop()停止。

因此,如果您想退出并完全关闭程序,您应该使用 root.destroy(),因为它会停止 mainloop()并销毁窗口及其所有小部件。

但是,如果您想运行一些无限循环,并且不想破坏 Tkinter 窗口,并且想在 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

参见 What is the difference between root.destroy() and root.quit()?

我认为你错误地理解了 Tkinter 的退出函数。这个函数不需要你定义。

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

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

那么,你应该使用’。保存这个文件并双击。用 pyw’文件来运行你的 GUI,这次,你可以通过点击按钮来结束 GUI,而且你还可以发现不会有不愉快的 DOS 窗口。(如果你运行’。Py’文件,则退出函数将失败。)

以防混乱。

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

破坏()停止主循环并杀死窗口,但让 python 运行

出口()停止整个过程

只是澄清一下,以防有人错过了 delete ()正在做的事情,OP 还询问如何“结束”tkinter 程序。

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

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.

The easiest way would be to click the red button (leftmost on macOS and rightmost on 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()

如果有人想绑定他们的 Escape 按钮来关闭整个 GUI:

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


def close(event):
sys.exit()


master.bind('<Escape>',close)
master.mainloop()

For menu bars:

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

而且你甚至不需要 exit ()函数,因为当你把它设置为命令时,它会退出整个程序。

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

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

or

from tkinter import*
root=Tk()
Button(root,text="exit",command=root.destroy).pack()
root.mainloop()

或者

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

我在这里创建了一个空白窗口 & 添加文件菜单选项在同一个窗口(根窗口) ,其中我只添加一个选项 出口

然后简单地 运行主回路

试着做一次

There is a simple one-line answer:

在命令中写入 exit()

就是这样!

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

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


我通常使用默认的 tkinter quit函数,但是你可以自己做,像这样:

from tkinter import *
from tkinter.ttk import *


window = Tk()
window.geometry('700x700') # 700p x 700p screen


def quit(self):
proceed = messagebox.askyesno('Quit', 'Quit?')
proceed = bool(proceed) # So it is a bool


if proceed:
window.quit()
else:
# You don't really need to do this
pass


btn1 = Button(window, text='Quit', command=lambda: quit(None))


window.mainloop()

你不必打开一个函数来关闭窗口,除非你正在做一些更复杂的事情:

from Tkinter import *


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