如何將 Tkinter 小元件居中對齊在粘性框架中?
Tkinter 具有大量用於配置 Tkinter 小元件屬性的內建函式和方法。這些屬性因不同的幾何管理器而異。網格幾何管理器是其中之一,它處理應用程式中的許多複雜佈局問題。網格幾何管理器將在給定空間(如適用)中新增所有小元件,而不會相互重疊。
假設我們已使用網格幾何管理器建立了粘性框架,並且希望在框架內居中顯示標籤文字小元件。在這種情況下,我們必須先透過配置行和列屬性來使主視窗變粘。主視窗一旦透過該框架變得粘性,就可以使任何小元件合理地調整大小。在這種情況下,標籤小元件必須具有粘性。現在,要居中對齊小元件,請指定行、列和權重的值。
示例
# Import the required library from tkinter import * # Create an instance of tkinter frame win= Tk() # Set the size of the Tkinter window win.geometry("700x350") # Add a frame to set the size of the window frame= Frame(win, relief= 'sunken') frame.grid(sticky= "we") # Make the frame sticky for every case frame.grid_rowconfigure(0, weight=1) frame.grid_columnconfigure(0, weight=1) # Make the window sticky for every case win.grid_rowconfigure(0, weight=1) win.grid_columnconfigure(0, weight=1) # Add a label widget label= Label(frame, text= "Hey Folks! Welcome to Tutorialspoint", font=('Helvetica 15 bold'), bg= "white") label.grid(row=3,column=0) label.grid_rowconfigure(1, weight=1) label.grid_columnconfigure(1, weight=1) win.mainloop()
輸出
執行上述程式碼將顯示一個居中放置在粘性框架內的標籤文字。
廣告