简体   繁体   English

Tkinter 网格与包管理器窗口缩小

[英]Tkinter grid vs pack manager window shrink

I have an example code that use the Tkinter grid manager for creating and allocating four squares:我有一个示例代码,它使用 Tkinter 网格管理器来创建和分配四个正方形:

upper=tk.Frame(root)
lower=tk.Frame(root)
canv1=tk.Canvas(upper,bg="blue")
canv2 = tk.Canvas(upper, bg="yellow")
canv3 = tk.Canvas(lower, bg="red")
canv4 = tk.Canvas(lower, bg="green")
canv1.pack(side='left', fill='both', expand=True)
canv2.pack(side='right', fill='both', expand=True)
canv3.pack(side='left', fill='both', expand=True)
canv4.pack(side='left', fill='both', expand=True)
upper.pack(side='top', fill='both', expand=True)
lower.pack(side='bottom', fill='both', expand=True)
root.mainloop()

After the main window is created, the squares are proportionally expanding and shrinking when the window size is changed by mouse dragging.创建主窗口后,通过鼠标拖动更改窗口大小时,正方形会按比例扩大和缩小。 Squares are always changed proportionally whenever the windows change their size by dragging and moving any of its edge or window corners.每当窗口通过拖动和移动其任何边缘或窗口角来更改其大小时,正方形总是按比例更改。

I'm trying to get this same effect with pack manager.我试图通过包管理器获得同样的效果。 So I have the code:所以我有代码:

root=tk.Tk()
upper=tk.Frame(root)
lower=tk.Frame(root)
canv1=tk.Canvas(upper,bg="blue")
canv2 = tk.Canvas(upper, bg="yellow")
canv3 = tk.Canvas(lower, bg="red")
canv4 = tk.Canvas(lower, bg="green")
canv1.pack(side='left', fill='both', expand=True)
canv2.pack(side='right', fill='both', expand=True)
canv3.pack(side='left', fill='both', expand=True)
canv4.pack(side='left', fill='both', expand=True)
upper.pack(side='top', fill='both', expand=True)
lower.pack(side='bottom', fill='both', expand=True)
root.mainloop()

When the pack manager is used the squares are only expanded proportionally, when the size of window is changed.当使用包管理器时,正方形仅在窗口大小改变时按比例扩展。 During the shrinking (by dragging some edge or corner), the squares not change their size proportionally.在收缩过程中(通过拖动一些边或角),正方形不会按比例改变它们的大小。 I would like to ask - is it possible to make the squares shrink proportionally while changing windows size using pack manager?我想问一下 - 是否可以在使用包管理器更改窗口大小的同时使正方形按比例缩小?

The packer tries to preserve the original size of the widgets as long as possible.打包程序尽可能长时间地保留小部件的原始大小。 If a widget isn't large enough to fit, it only shrinks the widget that won't fit in its preferred size.如果小部件不够大,无法容纳,它只会缩小不适合其首选尺寸的小部件。 Thus, if you shrink the window horizontally, the widgets on the right will shrink so that the size of the widgets on the left are preserved.因此,如果您水平缩小窗口,右侧的小部件将缩小,从而保留左侧小部件的大小。

I think the only workaround is to give every canvas a preferred size of 1x1.我认为唯一的解决方法是为每个画布提供 1x1 的首选尺寸。 If you do that, and then give your window as a whole a geometry (so that the entire window isn't just a couple pixels in size) you will get the behavior you want.如果你这样做,然后给整个窗口一个几何图形(这样整个窗口就不仅仅是几个像素大小),你会得到你想要的行为。

root.geometry("800x800")
...
canv1 = tk.Canvas(upper,bg="blue", width=1, height=1)
canv2 = tk.Canvas(upper, bg="yellow", width=1, height=1)
canv3 = tk.Canvas(lower, bg="red", width=1, height=1)
canv4 = tk.Canvas(lower, bg="green", width=1, height=1)

For this specific problem, I see no advantage to using pack over grid , since you are in fact creating a grid of widgets.对于这个特定问题,我认为使用pack而不是grid没有任何优势,因为您实际上是在创建一个小部件网格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM