在 Tkinter 中,widget.rowconfigure 或 widget.grid_rowconfigure 哪一個才是正確的?


使用 Tkinter 構建應用程式時,我們可以使用許多元件和小部件來擴充套件該應用程式。為了在應用程式中渲染這些小部件,我們使用幾何管理器。

幾何管理器負責配置小部件在視窗中的位置和大小。網格幾何管理器按行和列處理要放置的小部件。

如果要讓小部件跨越一個或多列,我們使用 widget.rowconfigure() 或 widget.grid_rowconfigure()。它採用 權重行/列值等引數。

widget.rowconfigure() 有時用在 widget.grid_rowconfigure() 的位置。使用這些方法將允許小部件具有可以在行和列中應用的權重屬性。

示例

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the window
win.geometry("700x350")

# Add a new Frame
f1=Frame(win, background="bisque", width=10, height=100)
f2=Frame(win, background="blue", width=10, height=100)

# Add weight property to span the widget in remaining space
f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")

win.columnconfigure(0, weight=1)
win.rowconfigure(1, weight=0)

win.mainloop()

輸出

執行上述程式碼將在視窗中顯示一些綵帶。這些綵帶可以賦予權重屬性,以便在給定中提供額外的空間。

更新日期:2021 年 6 月 18 日

5,000+ 次瀏覽

開啟你的 職業 生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.