Python Tkinter – 設定條目寬度 100%
Tkinter 條目小元件接受文字欄位中的單行使用者輸入。我們可以透過在其建構函式中提供預設屬性和值來更改條目小元件的屬性。
假設我們要為應用程式建立一個全寬條目小元件。有幾種方法可以做到這一點,但是如果我們考慮使用 Pack Geometry Manager 來顯示條目小元件的最簡單的情況,那麼我們完全可以透過新增fill(x 或 y)屬性來設定條目小元件的寬度。
示例
# Import the required library from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win= Tk() # Set the size of the window win.geometry("700x350") # Add bottom widgets in the application label= Label(win, text= "Enter Your Name") label.pack() # Add an entry widget entry= Entry(win) entry.pack(fill='x') win.mainloop()
輸出
執行上述程式碼將顯示一個全寬條目小元件,該小元件展開以覆蓋視窗中的 X 軸。
廣告