如何 從視窗中刪除 Tkinter 元件?
有時,我們希望刪除應用程式中無用的元件。我們可使用 Tkinter 中的 .destroy 方法從視窗或框架中刪除元件。可透過為此定義一個函式在元件中呼叫該方法。
示例
在這個示例中,我們建立了一個按鈕,它將元件文字標籤從視窗中刪除。
#Import the tkinter library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("650x450")
#Define a function to remove the text from the screen
def delete_text():
text.destroy()
#Create a text widget
text= Label(win,text="This is a New Line", font=('Aerial bold', 20))
text.pack(pady=20)
#Create a button for Deleting Widget
Button(win, text= "Click Here", font=('bold',20), command=
delete_text).pack(pady=10)
win.mainloop()輸出
執行以上程式碼將產生以下輸出 −

現在,單擊“在此處單擊”按鈕。它將從視窗中刪除 Label Text 元件。

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP