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

from Tkinter import *

def quit():
    # code to exit

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

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

有帮助吗?

解决方案

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

from Tkinter import *

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

<强>解释

root.quit()

上面一行 Bypasses 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

其他提示

def quit()
    root.quit()

def quit()
    root.destroy()
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()

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

首先,您应该按如下方式修改您的功能:

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

然后,您应该使用'.pyw'后缀来保存这些文件并双击'.pyw'文件来运行您的GUI,这次,您只需单击Button即可结束GUI,您可以还发现不会有令人不愉快的DOS窗口。 (如果运行'.py'文件,则退出函数将失败。)

退出Python程序的常用方法:

sys.exit()

(您也可以通过退出状态)或

raise SystemExit

可以在Tkinter程序中正常工作。

混淆时的照明......

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

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

B)exit()停止整个过程

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

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

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

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

您可以使用:

root.destroy()

或者

root.quit()

如果这不起作用,请将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()

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

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

def close(event):
    sys.exit()

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

你只需输入:

root.destroy()

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

def quit1():
     root.destroy()

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

我使用以下代码退出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()

试试这个:

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

试试这个。

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

也许您将root参数发送到您执行的框架。因此,如果你想完成它,你必须打电话给你的父亲,这样他就可以关闭它,而不是关闭他的每个孩子。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top