如何关闭 tkinter 窗口?
- 2025-02-27 09:06:00
- admin 原创
- 68
问题描述:
如何结束 Tkinter 程序?假设我有以下代码:
from Tkinter import *
def quit():
# code to exit
root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()
我应该如何定义quit
退出应用程序的函数?
解决方案 1:
您应该使用destroy()
来关闭 Tkinter 窗口。
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()
即,如果执行命令,root.mainloop()
它仍然会在后台运行。quit()
root.destroy()
当destroy()
命令消失时root.mainloop()
,即root.mainloop()
停止,<window>.destroy()
彻底销毁并关闭窗口。
因此,如果您想退出并完全关闭程序,您应该使用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
请参阅root.destroy() 和root.quit() 之间有什么区别?。
解决方案 2:
def quit()
root.quit()
或者
def quit()
root.destroy()
解决方案 3:
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:
混乱时照明……
def quit(self):
self.destroy()
exit()
A) destroy() 停止主循环并关闭窗口,但保持 Python 运行
B)exit()停止整个过程
只是为了澄清一下,以防有人错过了 destroy() 正在做什么,并且 OP 还询问如何“结束” tkinter 程序。
解决方案 5:
我认为你错误地理解了Tkinter的quit函数。该函数不需要你定义。
首先,您应该按如下方式修改您的函数:
from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.quit).pack()
root.mainloop()
然后,你应该使用 .pyw 后缀来保存这些文件,并双击 .pyw 文件来运行你的 GUI,此时,你可以单击按钮来结束 GUI,你还会发现不会有令人不快的 DOS 窗口。(如果你运行 .py 文件,退出功能将失败。)
解决方案 6:
退出Python程序的常用方法:
sys.exit()
(您也可以传递退出状态)或
raise SystemExit
可以在 Tkinter 程序中正常工作。
解决方案 7:
如果有人想将他们的 Esc 按钮绑定到关闭整个 GUI:
master = Tk()
master.title("Python")
def close(event):
sys.exit()
master.bind('<Escape>',close)
master.mainloop()
解决方案 8:
最简单的方法是单击红色按钮(在 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()
解决方案 9:
你只需要输入以下内容:
root.destroy()
您甚至不需要 quit() 函数,因为当您将其设置为命令时,它将退出整个程序。
解决方案 10:
你不必打开一个功能来关闭你的窗口,除非你正在做一些更复杂的事情:
from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
解决方案 11:
在idlelib.PyShell
模块中,root
类型变量Tk
被定义为全局变量
在函数的末尾,PyShell.main()
它调用root.mainloop()
一个无限循环的函数,并一直运行直到循环被root.quit()
函数中断。因此,root.quit()
只会中断mainloop
为了销毁与该idlelib窗口相关的所有小部件,root.destroy()
需要调用它,这是idlelib.PyShell.main()
函数的最后一行。
解决方案 12:
我通常使用默认的 tkinterquit
函数,但您可以执行自己的函数,如下所示:
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()
解决方案 13:
对于菜单栏:
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()
解决方案 14:
我使用以下代码退出 Tkinter 窗口:
from tkinter import*
root=Tk()
root.bind("<Escape>",lambda q:root.destroy())
root.mainloop()
或者
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()
解决方案 15:
下面是代码片段。我提供了一个小场景。
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()
我在这里创建了一个空白窗口并在同一窗口(根窗口)上添加了文件菜单选项,其中我只添加了一个选项退出。
然后只需为root运行 mainloop即可。
尝试做一次
解决方案 16:
当然,您可以按如下方式将命令分配给按钮,但是,如果您正在制作 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 ()
解决方案 17:
答案很简单,一行即可:
exit()
在命令中写入 -
就是这样!
扫码咨询,免费领取项目管理大礼包!