เราสามารถใช้วิดเจ็ต Label ของ Tkinter เพื่อแสดงข้อความและรูปภาพ โดยการกำหนดค่าวิดเจ็ต Label เราสามารถเปลี่ยนข้อความ รูปภาพ และคุณสมบัติอื่นๆ ของวิดเจ็ตได้แบบไดนามิก
เพื่ออัปเดตวิดเจ็ต Label แบบไดนามิก เราสามารถใช้ config(**options) หรือ วิธีการกำหนดค่าแบบอินไลน์ เช่น สำหรับอัปเดตข้อความ ใช้ Label["text"] = text; สำหรับลบวิดเจ็ต Label ใช้เมธอด pack_forget()
ตัวอย่าง
# Import the required libraries
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
def add_label():
global label
label = Label(win, text="1. A Newly created Label", font=('Aerial 18'))
label.pack()
def remove_label():
global label
label.pack_forget()
def update_label():
global label
label["text"] = "2. Yay!! I am updated"
# Create buttons for add/remove/update the label widget
add = ttk.Button(win, text="Add a new Label", command=add_label)
add.pack(anchor=W, pady=10)
remove = ttk.Button(win, text="Remove the Label", command=remove_label)
remove.pack(anchor=W, pady=10)
update = ttk.Button(win, text="Update the Label", command=update_label)
update.pack(anchor=W, pady=10)
win.mainloop()
การรันโค้ดด้านบนจะแสดงหน้าต่างพร้อมปุ่มบางตัว แต่ละปุ่มสามารถใช้เพื่ออัปเดต/ลบ หรือเพิ่ม label ในแอปพลิเคชันได้
ผลลัพธ์

เมื่อคลิกปุ่ม "Update the Label" label จะถูกอัปเดตดังนี้:
