Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Lấy chiều rộng widget Tkinter

Lấy chiều rộng widget bằng winfo_width() sau khi win.update() để render xong.

Ví dụ

# Import thư viện
from tkinter import *

# Tạo khung
win = Tk()
win.geometry("700x350")
win.config(bg='#aad5df')

# Label
label = Label(win, text="Hello World!", font=('Helvetica 18 bold'), background='white', foreground='purple1')
label.pack(pady=50)
win.update()

# Lấy chiều rộng
width = label.winfo_width()
print("The width of the label is:", width, "pixels")

win.mainloop()

Kết quả

Label hiển thị. Console in chiều rộng.

Lấy chiều rộng widget Tkinter

The width of the label is: 148 pixels