Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีลบข้อความจาก Label ใน Python

Tkinter เป็นไลบรารี Python สำหรับสร้างและพัฒนาแอปพลิเคชันแบบ GUI ในบทความนี้ เราจะดูวิธีลบข้อความจาก Label ที่มีข้อความอยู่

เพื่อลบข้อความจาก Label เราจะสร้างปุ่มที่เชื่อมโยงกับ Label เพื่อทำหน้าที่เป็น Trigger

ตัวอย่าง

# 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 Command
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()

ผลลัพธ์

การรันโค้ดข้างต้นจะสร้างปุ่มที่ใช้ลบข้อความจาก Label

วิธีลบข้อความจาก Label ใน Python

ตอนนี้ คลิกปุ่ม "Remove Text" จะลบข้อความจาก Label และจะได้หน้าจอดังนี้:

วิธีลบข้อความจาก Label ใน Python