如何透過按一個按鈕關閉 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()輸出
執行上述程式碼將顯示一個視窗,其中有一個按鈕,可以觸發該按鈕以關閉視窗或框架。

現在,單擊“關閉”按鈕以關閉視窗。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP