gpt4 book ai didi

python - 在什么情况下 Tkinter 的 grid() 不能与 pack() 混合?

转载 作者:行者123 更新时间:2023-11-30 22:03:26 24 4
gpt4 key购买 nike

我想消除奇怪的额外空间,这些空间在使用 grid() 时似乎无法在布局中进行任何尺寸调整。独自一人,但打电话 pack()有时会让事情变得更糟:GUI 完全消失了!

我从@Bryan Oakley 读到了一些令人大开眼界的布局答案,例如:

When to use pack or grid layouts in tkinter?

Tkinter: grid or pack inside a grid?

但是当我静下心来写自己的东西时,我仍然经常遇到麻烦。

我的理解:

  1. 我必须有一个框架来填充 root window ,否则就没有希望填充窗口中的额外空间,但我单独调整小部件。
  2. 对于位于共同父级内的所有子级小部件 Frame ,我必须使用 pack()grid()但不能两者兼而有之。
  3. 使用grid()时在框架中,必须指定 Frame.grid_rowconfigure().grid_columnconfigure()非零 weight论据。否则什么也不会显示。
  4. 因此可以有主 Frame使用pack() ,但它的直接子项 Frames全部使用grid() ;在每个 child 的内心Frames在网格上,我们可以 pack()他们自己的子部件。换句话说,我们可以交织 grid()pack()按“区域”或容器层次结构级别,但切勿将它们混合在同一个容器中:唯一的限制。
  5. 通过仔细的重量设计,我可以填充父级的水平空间 Frame带着 child Frame充满水平布局的小部件,例如,所有小部件都使用 grid(sticky='nsew')和 child Frame使用pack(side='top', fill='both', expand=True) .

如果我的理解是正确的,那么我永远无法弄清楚为什么#5对我不起作用,例如,我的水平 child 的右端总是有未使用的额外空间 Frame里面主要Frame根窗口的。

更新2

我明白了。 #5 对我不起作用,因为我忘记指定 .grid_columnconfigure(0, weight=1)主要Frame使用前grid() 。我的错!案件已结。

更新

我在 macOS High Sierra 上运行 python 3.6.4 Homebrew。

最佳答案

In what cases Tkinter's grid() cannot be mixed with pack()?

在所有情况下,对于具有公共(public)母版的小部件,您不能同时使用 gridpack。在母版中,所有直接子项必须使用相同的几何管理器。在整个应用程序中,您可以根据需要混合使用 packgrid,只要您遵循一条规则,即不能将它们同时用于具有以下功能的小部件:同一个 parent 。

  1. I must have a Frame to fill the root window, otherwise there'd be no hope to fill the extra space in the window, however I tweak widgets alone.

这是不正确的。您无需使用框架即可轻松填充根窗口中的所有空间。

  1. For all the child widgets sitting inside a common parent Frame, I must use either pack() or grid() but not both.

这是正确的。第三种选择是使用 place,尽管它很少是最佳选择。

  1. When using grid() in a Frame, it's mandatory to specify Frame.grid_rowconfigure() and .grid_columnconfigure() with non-zero weight arguments. Otherwise, nothing would show up.

事实并非如此——将行和列配置为具有非零权重并不是强制性的。这通常是最佳实践,但并不是显示小部件所必需的。权重仅适用于网格如何管理额外空间。无论您是否使用权重,任何大小非零的小部件都应该出现。

  1. It's thus possible to have the main Frame using pack(), but its immediate child Frames all using grid()

正确。

  1. By a careful weight design, I could fill a horizontal space in a parent Frame with a child Frame full of widgets laid out horizontally, e.g., all widgets use grid(sticky='nsew'), and the child Frame uses pack(side='top', fill='both', expand=True).

这是正确的。

关于python - 在什么情况下 Tkinter 的 grid() 不能与 pack() 混合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53550789/

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