如何通过按一个按钮关闭 Tkinter 窗口?
Tkinter 最初会创建一个窗口或框架,其中包含部件和标签。我们假设我们要关闭窗口。按钮是用户界面部件,可用于执行某些操作。
示例
在此,我们将创建一个关闭 Tkinter 窗口的按钮。为了关闭和终止 TCL 解释器,我们主要使用 destroy() 方法。
#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("600x250") #Create a Label Label(win, text="Press the Close Button to close the window", font=('Helvetica bold', 11)).pack(pady=20) #Define a function to close the window def close_win(): win.destroy() #Create a Button for closing the window button= Button(win, text="Close", command=close_win) button.pack(pady=20) win.mainloop()
输出
运行上述代码将显示一个窗口,其中有一个按钮,可以触发该按钮以关闭窗口或框架。
现在,单击“关闭”按钮以关闭窗口。
广告