如何在 Python 中從標籤中刪除文字?


Tkinter 是一個用於建立和開發基於 GUI 的應用程式的 Python 庫。在本文中,我們將學習如何從標籤中刪除文字,該標籤中有一些文字。

要從標籤中刪除文字,我們將建立一個關聯的按鈕,該按鈕將充當該標籤的觸發器。

示例

#import Tkinter Library
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Define the size and geometry of the frame
win.geometry("700x400")

#Create a function for the Button Comman

def remove_text():
   text.config(text=" ")

#Create a text Label
text= Label(win, text= "www.tutorialspoint.com", font= ("Poppins", 30))
text.pack(pady=20)

#Create a Button

my_button= Button(win, text= "Remove Text", command= remove_text)
my_button.pack(pady=10)

win.mainloop()

輸出

執行上述程式碼將建立一個按鈕,可用於從標籤中刪除文字。

現在,單擊“移除文字”按鈕。它將從標籤中刪除文字,我們將獲得以下螢幕。

更新於: 06-Mar-2021

3 千+ 瀏覽

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.