Tkinter 网格布局不会展开

Tkinter grid layout won't expand

我正在尝试设计侧边栏,它应该看起来像大多数 Google 的侧边栏菜单(例如 Inbox 或 Play Music,浏览器版本)。 我正在使用网格布局,但它不会展开。我稍微查了一下,尽管添加了 sticky = "nesw"rowconfigure(0, weigth = 1),但右侧的框架不会扩展并填充根。

from tkinter import *
from tkinter import ttk

buttons = ["Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button"]  # List of contacts

root = Tk()
root.title('MyApp v0.1')
root.geometry('800x600')
#root['bg'] = "white"

# Side menu
frame = Frame(root, background = "white")
for i in range(len(buttons)):
    Button(frame, background = "white", text = buttons[i], command = None).grid(row = i, column = 0)
frame.rowconfigure(0, weight = 1)
frame.grid(row = 0, column = 0, sticky = "nesw")

sep = ttk.Separator(root, orient = "vertical")
sep.rowconfigure(0, weight = 1)
sep.columnconfigure(1, weight = 1)
sep.grid(row = 0, column = 1, padx = 5, sticky = "nesw")

root.mainloop()

这是我得到的。我左边的按钮应该在白色背景框架上。 Frame 和分隔符应该放在应用程序的底部 window.

提前致谢。

如果您希望按钮的容器从 window 的顶部一直延伸到底部,请尝试在根上调用 rowconfigure。如果您希望按钮均匀分布,而不是调用 frame.rowconfigure(0, weight = 1),请为从 0 到 len(buttons) 的帧的 each 行调用 frame.rowconfigure .

from tkinter import *
from tkinter import ttk

buttons = ["Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button", "Button"]  # List of contacts

root = Tk()
root.title('MyApp v0.1')
root.geometry('800x600')
#root['bg'] = "white"
root.rowconfigure(0, weight=1)

# Side menu
frame = Frame(root, background = "white")
for i in range(len(buttons)):
    Button(frame, background = "white", text = buttons[i], command = None).grid(row = i, column = 0)
    frame.rowconfigure(i, weight = 1)
frame.grid(row = 0, column = 0, sticky = "nesw")

sep = ttk.Separator(root, orient = "vertical")
sep.rowconfigure(0, weight = 1)
sep.columnconfigure(1, weight = 1)
sep.grid(row = 0, column = 1, padx = 5, sticky = "nesw")

结果: