Tkinter 中关闭窗口的功能
无论何时我们运行一个 Tkinter 应用程序,它都会显示一个基于 GUI 的窗口,其中包含小组件、框架和其他元素。假设我们希望使用一个函数来关闭我们的应用程序。Python Tkinter 中的 destroy() 方法用于在 mainloop 函数之后终止应用程序的正常执行。
示例
在此示例中,将创建一个button对象,触发它以关闭应用程序。
#Import the tkinter library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("650x250")
#Define a function
def close_app():
win.destroy()
#Create a text Label
Label(win, text= "Click to close the Window", font= ('Helvetica bold', 14)).pack(pady=20)
#Create a Button for closing the window
button= Button(win, text= "Close", command= close_app, font=('Helvetica bold', 10))
button.pack(pady=10)
win.mainloop()输出
运行以上代码会显示一个包含一个按钮的窗口,该按钮可用于关闭窗口。

现在,你可以单击“关闭”按钮以关闭窗口。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP