สมมติว่าสำหรับแอปพลิเคชันเฉพาะ เราต้องการดึงค่าปุ่มตามชื่อ ในกรณีเช่นนี้ เราสามารถใช้ .cget() การทำงาน. วิดเจ็ต tkinter ทุกตัวรองรับ .cget() เนื่องจากสามารถใช้เรียกข้อมูลการกำหนดค่าวิดเจ็ต เช่น ค่าหรือชื่อได้
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้างปุ่มแล้วเก็บข้อความของปุ่มไว้ในตัวแปร "mytext" เมื่อใช้ตัวแปร เราจะแสดงข้อความในวิดเจ็ตป้ายกำกับ
#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Create a button
button= ttk.Button(win, text="My Button")
button.pack()
#Get the text of Button
mytext= button.cget('text')
#Create a label to print the button information
Label(win, text=mytext, font= ('Helvetica 20 bold')).pack(pady=20)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีปุ่มและป้ายกำกับข้อความที่แสดงข้อความของปุ่ม
