gpt4 book ai didi

python - Tkinter 网格布局不会扩展

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:42 33 4
gpt4 key购买 nike

我正在尝试设计侧边栏,它应该看起来像大多数 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()

这是我得到的。我左边的按钮应该在白色背景框架上。框架和分隔符应位于应用程序窗口的底部。

Result display

提前致谢。

最佳答案

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

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")

结果:

enter image description here

关于python - Tkinter 网格布局不会扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31885234/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com