以畫素為單位指定 Tkinter 文字框的尺寸
你可以使用 place(**option) 幾何管理器透過指定文字框的尺寸來設定其位置。在一個框架內例項化一個文字框會使文字框在整個應用程式視窗中保持獨立性。然後我們使用 place() 幾何管理器來分配文字框在視窗內的寬度和高度。畫素決定了文字框在視窗中的定位精確度。因此,place() 幾何管理器提供了一個網格系統,我們可以在其中將任何文字框放置在特定位置。
示例
# Import required libraries from tkinter import * from tkinter import ttk from lorem_text import lorem # Create an instance of tkinter frame win= Tk() # Set the window size win.geometry("700x350") # Add a Text widget and insert some dummy text text= Text(win, wrap= WORD, font= ('Courier 15 bold')) text.insert(END,lorem.sentence()) text.place(x=10, y= 10, width= 400, height= 300) win.mainloop()
輸出
當我們執行上面的程式碼時,一個文字框會顯示在視窗中,其中包含一些虛擬文字。文字框的尺寸可以透過更改 place() 幾何管理器中 x、y、width 和 height 的值來更新。
廣告