1

I have an example code that use the Tkinter grid manager for creating and allocating four squares:

root=tk.Tk()
root.rowconfigure(0,weight=1)
root.rowconfigure(1,weight=1)
root.columnconfigure(0,weight=1)
root.columnconfigure(1,weight=1)
canv1=tk.Canvas(root, bg="blue")
canv2 = tk.Canvas(root, bg="yellow")
canv3 = tk.Canvas(root, bg="red")
canv4 = tk.Canvas(root, bg="green")
canv1.grid(row=0, column=0, sticky="nsew")
canv2.grid(row=0, column=1, sticky="nsew")
canv3.grid(row=1, column=0, sticky="nsew")
canv4.grid(row=1, column=1, sticky="nsew")
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?

Gregosh
  • 17
  • 7
  • 1
    In your example I only see the `pack()` method, but you are mentioning `grid()`. – Rory Jul 22 '22 at 13:51
  • The first part of code uses grid() (the first one). And with grid everything is shrinking and expanding in good way. I would like to make the same effect with pack(). So I give the example of my best try what I was able to figure out so far. And with pack() squares are expanding but not shrinking. So it is possible to do with pack()? – Gregosh Jul 22 '22 at 14:11
  • Of course You are right. Thank You - I made mistake during pasting examples. I pasted the correct one for the future. – Gregosh Jul 25 '22 at 11:07

1 Answers1

2

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. 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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Of course You are right. In this examples there is no difference between using pack() and grid(). But I'm at the learing stage - so I would like create bigger gui in tkinter and I'm trying to learn properties of layout managers. Anyway, when the flags: width and height in canvX are added the behaviour is correct - the same as in the grid. Elements are expanding and shrinking proportionally acording to changing window size. Thank You again. – Gregosh Jul 25 '22 at 11:12