当窗口大小调整时调整 Tkinter 列表框小部件的大小
我是 Tkinter 的新手,我有一个 Listbox
小部件,我希望在更改主窗口大小时自动调整大小。
本质上我想要一个流体高度/宽度列表框。如果有人可以向我指出一些文档或提供一些代码/见解,我将不胜感激。
I'm new to Tkinter, and I've got a Listbox
widget that I'd like to automatically-resize when changing the main window's size.
Essentially I would like to have a fluid height/width Listbox. If someone can point me to some documentation or provide a bit a code / insight, I'd appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要阅读几何管理器 pack 和 grid,它允许您将小部件放置在窗口中并指定它们是否放大和缩小。还有第三个几何管理器,place,但它并不经常使用。
这是一个简单的示例:
如果您希望使用
grid
而不是pack
,请删除调用pack
的两行并用这四行替换它们:请注意,使用网格时,您必须采取额外的步骤来配置包含列表框的行和列的权重,否则 tkinter 不会为小部件分配任何额外的空间。
You want to read up on the geometry managers pack and grid, which lets you place widgets in a window and specify whether they grow and shrink or not. There's a third geometry manager, place, but it's not used very often.
Here's a simple example:
If you wish to use
grid
instead ofpack
, remove the two lines that callpack
and replace them with these four lines:Note that with grid you have to take the extra step to configure the
weight
for the row and column that contains the listbox, otherwise tkinter won't allocate any extra space to the widget.调整窗口大小时允许列表框拉伸的两种主要方法是使用 .pack() 或 .grid() 方法。
.pack()
我发现最简单的方法是使用
.pack()
方法,并利用fill =
&expand=True
选项。如果您的列表框放置在框架中,则该框架还需要使用
fill=
&expand=True
选项。.grid()
另一种技术是使用
.grid()
方法并利用sticky=
选项。此外,您还需要配置列表框所在的行和列。粘性
选项使列表框粘住 拉伸时单元格的“北”(上)、“南”(下)、“东”(右)和“西”(左)侧。如果您的列表框放置在框架内,则需要配置框架所在的列和行 ,并配置列表框所在的列和行
。 .grid()
现在还有另一种技术,但有些人对此不以为然。第三种技术是在同一脚本中使用
.pack()
方法和.grid()
方法。您可以在同一脚本中混合不同的几何管理方法,只要每个容器仅使用一种管理类型即可。您可以在下面看到这样的示例。您可以在上面看到框架使用
.pack()
而列表框和按钮使用.grid()
。这是可能的,因为框架驻留在根容器内,而列表框和按钮驻留在各自的框架内。检查您的 tkinter 版本使用情况:
如果您想了解 fill 和 expand 之间的区别,请参阅以下链接。
https://effbot.org/tkinterbook/pack.htm
The two main ways to allow a listbox to stretch when the window is resized are using the .pack() or .grid() methods.
.pack()
I found the easiest way to do this is by using the
.pack()
method, and utilizing thefill=
&expand=True
options.If your listbox is placed in a frame, the frame will also need to use the
fill=
&expand=True
options..grid()
The alternative technique is to use the
.grid()
method and utilize thesticky=
option. In addition, you will need to configure the row and column that the listbox resides in.The
sticky
option causes the listbox to stick to the "North" (Top), "South" (Bottom), "East" (Right), and "West" (Left) sides of the cell as it is stretched.If your listbox is placed within a frame, you will need to configure the column and row that the frame is in, along with configure the column and row that the listbox is in.
.pack() & .grid()
Now there is one other technique, but some people frown on it. The third technique is to utilize the
.pack()
method and.grid()
method in the same script. You can mix different geometry management method in the same script as long as only a one management type is used per container. You can see an example of this below.You can see above that the frames used
.pack()
while the listbox and buttons used.grid()
. This was possible because the frames resided within the root container, while the listbox and buttons resided within their respective frames.To check you tkinter version use:
If you would like to learn about the differences between fill and expand, please see the following link.
https://effbot.org/tkinterbook/pack.htm