Tkinter 打包方法混淆 [英] Tkinter pack method confusion

查看:20
本文介绍了Tkinter 打包方法混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么打包管理器不允许您在顶部打包的小部件下方打包左和右.以下代码的预期输出是

I cant understand why the pack manager wont let you pack LEFT AND RIGHT below top packed widgets. My expected output of the following code would be

 A
 B
C D 
 E

但它只显示:

 A
 B
C D

import tkinter as tk

root = tk.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.state('zoomed')
root.geometry("%dx%d+0+0" % (w-15, h-75))
A = tk.LabelFrame(root,text='A', bd=2)
B = tk.LabelFrame(root,text='B', bd=2)
C = tk.LabelFrame(root,text='C', bd=2)
D = tk.LabelFrame(root,text='D', bd=2)
E = tk.LabelFrame(root,text='E', bd=2)

A.pack(fill=tk.BOTH, expand=tk.TRUE)
B.pack(fill=tk.BOTH, expand=tk.TRUE)
C.pack(side=tk.LEFT,fill=tk.BOTH, expand=tk.TRUE)
D.pack(side=tk.RIGHT,fill=tk.BOTH, expand=tk.TRUE)
E.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=tk.TRUE)

root.mainloop()

推荐答案

简而言之,packer 并非旨在直接创建您要创建的布局.你可以做到,但它需要一些额外的帧.在这种特殊情况下,您显然是在尝试创建网格,grid 可以说是更好的选择.

In short, the packer isn't designed to directly create the layout you're trying to create. You can do it, but it requires some extra frames. In this particular case where you're clearly trying to create a grid, grid is arguably the better choice.

在解释为什么会这样之前,让我提出一个解决方案.创建另一个名为CD"的框架,用于保存 CD.然后,您可以将 C 包装在左侧,将 D 包装在右侧,然后将此框架放置在 B 下方.这就是您使用封隔器实现所需结果的方式.

Before I get to an explanation for why this is so, let me suggest a solution. Create another frame called "CD" which will be used to hold both C and D. You can then pack C to the left and D to the right, and then place this frame below B. That is how you achieve the desired result using the packer.

A = tk.LabelFrame(root,text='A', bd=2)
B = tk.LabelFrame(root,text='B', bd=2)
CD = tk.Frame(root)
C = tk.LabelFrame(CD,text='C', bd=2)
D = tk.LabelFrame(CD,text='D', bd=2)
E = tk.LabelFrame(root,text='E', bd=2)

C.pack(side=tk.LEFT,fill=tk.BOTH, expand=tk.TRUE)
D.pack(side=tk.RIGHT,fill=tk.BOTH, expand=tk.TRUE)

A.pack(fill=tk.BOTH, expand=tk.TRUE)
B.pack(fill=tk.BOTH, expand=tk.TRUE)
CD.pack(fill=tk.BOTH, expand=tk.TRUE)
E.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=tk.TRUE)

解释包装器

packer 的工作方式是在未分配空间的一侧保留一块空间.这个分配的空间在将小部件打包到顶部或底部时占据了未分配空间的整个宽度,而在左侧或右侧打包时占据了整个高度.这是一个重要的细节.

让我试着用一些截图来描述它.在所有情况下,以下屏幕截图均未显示帧,而是显示了打包程序管理的已分配和未分配空间量.

Let me try to describe it with some screenshots. In all cases, the following screenshots do not show the frames, they show the amount of allocated and unallocated space being managed by the packer.

当你沿着顶部打包A(如果你不指定边,顶部是默认的),一块空间被保留给整个宽度根窗口.而且因为你把它打包到了顶部,唯一剩下的空间在 A 下方.从现在开始,A 的左边或右边都不能打包.

When you pack A along the top (top is the default if you don't specify a side), a parcel of space is reserved for the full width of the root window. And because you packed it to the top, the only remaining space is below A. Nothing can be packed to the left or right of A from this point on.

(实际上,您可以使用一些高级选项将东西放在左侧或右侧,但这超出了本说明的范围)

(Actually, you can put things to the left or right by using some advanced options, but that's beyond the scope of this description)

当你把 B 放在顶部时,唯一可用的空间在 A 下方.同样,因为您将它打包在顶部,在打包 B 之后,现在唯一可用的可用空间将低于 B.

When you pack B along the top, the only available space is below A. Again, because you packed it along the top, after packing B the only available free space now will be below B.

当您沿左侧打包C时,会为剩余空间的整个高度保留一块空间.这意味着在 C 下不能放置任何东西.剩下的所有空间都在C的右边,B的下面.

When you pack C along the left, a parcel of space is reserved for the full height of the remaining space. That means nothing will be able to be placed below C. All of the remaining space is to the right of C, and below B.

  1. 当你打包D时,它必须位于C的右侧和B的下方.因为你把它包装在右边,它会保留 D 上下所有剩余的垂直空间.
  1. When you pack D, it must go to the right of C and below B. Because you pack it to the right, it will reserve all remaining vertical space above and below D.

当你把 E 放在底部时,它唯一可以去的地方是 B 下方,C 的右侧,以及D 的左边.

When you pack E along the bottom, the only place it can go is below B, to the right of C, and to the left of D.

由于封隔器沿边分配所有空间的性质,封隔器在所有小部件放置在同一轴上时效果最佳 - 无论是垂直堆叠还是水平堆叠.当您想像使用 CD 那样切换轴时,通常最好将它们放在单独的框架中.在该框架内,您可以从左到右放置小部件,但该框架可以像其他小部件一样保持在自上而下的轴上.

Because of the nature of the packer allocating all space along a side, the packer works best when all widgets are placed on the same axis - either stacked vertically or horizontally. When you want to switch the axis as you did with C and D, its usually best to put them in side a separate frame. Within that frame you can put widgets left-to-right, but the frame can remain in the top-to-bottom axis as the other widgets.

注意:这是您的代码中实际发生的情况:E 被挤压在 CD 之间.由于各种原因,它不可见,但如果您给它一个明确的大小和颜色,它就会显示出来.

Note: this is what is actually happening in your code: E is being squeezed between C and D. It's not visible for various reasons, but if you were to give it an explicit size and color then it would show up.

packer 算法的权威描述见tcl/tk 包的手册页.

The authoritative description of the packer algorithm can be found in the tcl/tk man pages for pack.

这篇关于Tkinter 打包方法混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆