如何在 tkinter 中创建自动调整大小的按钮网格?
我正在尝试使用 Tkinter 创建按钮网格(以实现可点击的单元格效果)。
我的主要问题是我无法使网格和按钮自动调整大小并适合父窗口。
例如,当网格上有大量按钮时,我不会缩小按钮以使网格适合窗口,而是会得到一个超出屏幕的拉伸框架。
我正在寻找的效果是网格填充所有可用空间,然后调整其单元格大小以适合该空间。我已阅读文档,但我仍然不知道如何使其工作。
这是基本代码,这是我的起点:
def __init__(self):
root = Tk()
frame = Frame(root)
frame.grid()
#some widgets get added in the first 6 rows of the frame's grid
#initialize grid
grid = Frame(frame)
grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
#example values
for x in range(60):
for y in range(30):
btn = Button(grid)
btn.grid(column=x, row=y)
root.mainloop()
I am trying to create a grid of buttons(in order to achieve the clickable cell effect) with Tkinter.
My main problem is that I cannot make the grid
and the buttons autoresize and fit the parent window.
For example, when I have a high number of buttons on the grid, instead of shrinking the buttons so that the grid fits inside the window, I get a stretched frame that goes off screen.
The effect that I am looking for is the grid filling all available space, then resizing its cells to fit within that space. I have read at the documentation, but I still cannot figure out how to make it work.
This is the basic code which is my starting point:
def __init__(self):
root = Tk()
frame = Frame(root)
frame.grid()
#some widgets get added in the first 6 rows of the frame's grid
#initialize grid
grid = Frame(frame)
grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
#example values
for x in range(60):
for y in range(30):
btn = Button(grid)
btn.grid(column=x, row=y)
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将行和列配置为具有非零权重,以便它们占用额外的空间:
您还需要配置按钮,以便它们展开以填充cell:
这必须一直完成,所以这里是一个完整的示例:
You need to configure the rows and columns to have a non-zero weight so that they will take up the extra space:
You also need to configure your buttons so that they will expand to fill the cell:
This has to be done all the way up, so here is a full example:
@Vaughn Cato 在这里给出了一个很好的答案。然而,他不小心在他的示例中包含了一堆无关的代码。这是一个经过清理且更有组织的完整示例,其功能与他的示例完全相同。
屏幕截图:
首次打开时(小):
最大化窗口后:
TODO
from tkinter import *
,并使用import tkinter
代替,如下全部导入是不好的做法。最好明确地看到在哪里
每个对象都来自显式使用模块的名称空间。
@Vaughn Cato gave an excellent answer here. However, he has accidentally included a bunch of extraneous code in his example. Here is a cleaned up and more organized full example doing exactly what his example does.
Screenshots:
When it first opens (small):
After you maximize the window:
TODO
from tkinter import *
, and useimport tkinter
instead, asit's bad practice to import all. It's better to see explicitly where
each object comes from by using a module's namespace explicitly.
要使按钮在窗口最大化时展开,请尝试修改button.grid条目,如下所示:
To make the buttons expand when the window is maximized, try to modify the button.grid entry as follows:
当您使用滚动条时,网格权重方法可能不起作用(至少在 Mac 中),因此在使用它时,将小部件和滚动条打包在框架内,然后对框架进行网格化。
The grid weight method might not work when you use scrollbar (at least in Mac) , so while using it pack the widget and scrollbar inside a frame and then grid the frame instead.