如何獲取 Tkinter 視窗小部件的寬度?
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()輸出
執行以上程式碼將顯示一個包含 Label 視窗小部件的視窗。

當我們編譯程式碼時,它將在控制檯上列印標籤視窗小部件的寬度。
The width of the label is: 148 pixels
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP