在 Tkinter(Python)中,root.destroy() 和 root.quit() 有什么区别?
当我们对 tkinter 窗口对象调用 destroy() 方法时,它将终止 mainloop 进程并销毁窗口中的所有小部件。Tkinter destroy() 方法主要用于终止销毁在后台运行的解释器。
然而,可以使用 quit() 方法来停止 mainloop() 函数之后的进程。我们可以通过创建一个按钮对象来演示这两种方法的功能。
示例
#Import the required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Set the geometry of frame
win.geometry("650x450")
#Define a function for Button Object
def quit_win():
win.quit()
def destroy_win():
win.destroy()
#Button for Quit Method
Button(win,text="Quit", command=quit_win, font=('Helvetica
bold',20)).pack(pady=5)
#Button for Destroy Method
Button(win, text= "Destroy", command=destroy_win, font=('Helvetica
bold',20)).pack(pady=5)
win.mainloop()输出
运行代码将显示一个窗口,其中分别有两个按钮“退出”和“销毁”。

警告 - quit() 会突然终止应用程序,因此建议你在执行后从管理器中关闭应用程序。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP