Tkinter 使网格适合框架大小

Tkinter Make Grid Fit Frame Size

我想知道是否有办法使 grids rowscolumns 适合所设置的 Frame 的大小。因此,如果我创建一个 Frame 并设置其大小,则 grid 将适合 Frame.

的大小

这是我到目前为止尝试过的:

window = Tk()

frame = Frame(window)
frame.place(x=0, y=0, width=200, height=200)
title = Label(frame, text="The Title", bg="cornflower blue", height=2)
title.grid(row=0, column=0, sticky=NSEW)
sort_button = Button(frame, text="The Button", relief="groove", height=2, bg="maroon1",)
sort_button.grid(row=1, column=0, sticky=NSEW)

window.mainloop()

For example this is the output of my current code:

But this is what I want:

您可以为框架的列设置权重。

from tkinter import *

window = Tk()

frame = Frame(window,bg="yellow")

...

frame.grid_columnconfigure(0, weight=1)

window.mainloop()