วิดเจ็ต Tkinter ควรจะปรากฏในหน้าต่างแอปพลิเคชัน Tkinter วิดเจ็ตทั้งหมดสามารถกำหนดค่าและปรับแต่งได้โดยใช้คุณสมบัติหรือฟังก์ชันที่กำหนดไว้ล่วงหน้า
ในการรับความกว้างของวิดเจ็ตในแอปพลิเคชัน Tkinter เราสามารถใช้ winfo_width() กระบวนการ. ส่งคืนความกว้างของวิดเจ็ตซึ่งสามารถพิมพ์ได้ในภายหลังเป็นเอาต์พุต
ตัวอย่าง
#Import the required libraries
from tkinter import *
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry
win.geometry("700x350")
#Set the default color of the window
win.config(bg='#aad5df')
#Create a Label to display the text
label=Label(win, text= "Hello World!",font= ('Helvetica 18 bold'), background= 'white', foreground='purple1')
label.pack(pady = 50)
win.update()
#Return and print the width of label widget
width = label.winfo_width()
print("The width of the label is:", width, "pixels")
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีวิดเจ็ตป้ายกำกับ

เมื่อเราคอมไพล์โค้ด มันจะพิมพ์ความกว้างของวิดเจ็ตฉลากบนคอนโซล
The width of the label is: 148 pixels