【发布时间】:2010-09-11 18:35:15
【问题描述】:
如何结束 Tkinter 程序?假设我有这个代码:
from Tkinter import *
def quit():
# code to exit
root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()
我应该如何定义quit 函数来退出我的应用程序?
【问题讨论】:
如何结束 Tkinter 程序?假设我有这个代码:
from Tkinter import *
def quit():
# code to exit
root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()
我应该如何定义quit 函数来退出我的应用程序?
【问题讨论】:
这是你必须做的销毁一个窗口:
name_of_window.destroy()
【讨论】:
你不必打开一个函数来关闭你的窗口,除非你正在做一些更复杂的事情:
from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
【讨论】:
我通常使用默认的 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()
【讨论】:
当然你可以将命令分配给按钮,但是,如果你正在做一个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 ()
【讨论】:
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 中缺少),而且您调用的函数也错误。
有一个简单的单行答案:
在命令中写入 - exit()
就是这样!
【讨论】:
raise SystemExit
这在第一次尝试时有效, 在哪里
self.destroy()
root.destroy()
没有
【讨论】:
root.destroy 可以使用。root.quit 也可以使用。
就我而言
quitButton = Button(frame, text = "Quit", command = root.destroy)
希望对你有帮助。
【讨论】:
下面的代码 sn-p。我提供了一个小场景。
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。
试着做一次
【讨论】:
def quit()
root.quit()
或
def quit()
root.destroy()
【讨论】:
root.destroy(),因为它会终止主程序循环。见:http://www.daniweb.com/software-development/python/threads/66698
我使用以下代码退出 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()
【讨论】:
你只需要输入这个:
root.destroy()
而且你甚至不需要 quit() 函数,因为当你将它设置为命令时它会退出整个程序。
【讨论】:
root.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()
【讨论】:
如果有人想将他们的 Escape 按钮绑定到关闭整个 GUI:
master = Tk()
master.title("Python")
def close(event):
sys.exit()
master.bind('<Escape>',close)
master.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()
【讨论】:
import sys
from Tkinter import *
def quit():
sys.exit()
root = Tk()
Button(root, text="Quit", command=quit).pack()
root.mainloop()
应该按照你的要求去做。
【讨论】:
您应该使用destroy() 关闭 tkinter 窗口。
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
【讨论】:
quit 将销毁所有小部件;如果小部件被销毁,mainloop 将退出。
def quit1():
root.destroy()
Button(root, text="Quit", command=quit1).pack()
root.mainloop()
【讨论】:
我认为你错误地理解了 Tkinter 的退出功能。这个函数不需要你定义。
首先,你应该如下修改你的函数:
from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.quit).pack()
root.mainloop()
然后,你应该使用'.pyw'后缀来保存这个文件并双击'.pyw'文件来运行你的GUI,这一次你可以通过点击按钮结束GUI,你可以也发现不会出现令人不快的DOS窗口。 (如果你运行 '.py' 文件,退出函数会失败。)
【讨论】:
你可以使用:
root.destroy()
或者
root.quit()
如果这不起作用,请将 root 更改为程序开始时的变量
import tkinter
main = Tk()
main.destroy()
main.mainloop
【讨论】:
在idlelib.PyShell 模块中,root 类型的变量Tk 被定义为全局变量
在PyShell.main() 函数结束时,它调用root.mainloop() 函数,这是一个无限循环,它一直运行到循环被root.quit() 函数中断。因此root.quit()只会中断mainloop的执行
为了销毁与该 idlelib 窗口相关的所有小部件,需要调用 root.destroy(),这是 idlelib.PyShell.main() 函数的最后一行。
【讨论】:
试试这个:
from Tkinter import *
import sys
def exitApp():
sys.exit()
root = Tk()
Button(root, text="Quit", command=exitApp).pack()
root.mainloop()
【讨论】:
在混乱的情况下照明......
def quit(self):
self.destroy()
exit()
A) destroy() 停止主循环并杀死窗口,但让 python 继续运行
B) exit() 停止整个过程
只是为了澄清以防有人错过了 destroy() 在做什么,并且 OP 还询问了如何“结束” tkinter 程序。
【讨论】:
试试这个。
self.parent.destroy()
self.parent.quit()
也许您将类似 root 的参数发送到您所做的框架。所以如果你想完成它,你必须打电话给你的父亲,这样他就可以关闭它,而不是关闭他的每个孩子。
【讨论】:
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()
【讨论】:
退出Python程序的常用方法:
sys.exit()
(您也可以向其传递退出状态)或
raise SystemExit
在 Tkinter 程序中可以正常工作。
【讨论】: