I'm in a situation where I must use .pack()
or .grid()
alone to achieve specified widget sizes when they are laid out next to each other.
I used to mix .pack()
with .grid()
based on the knowledge gained in this post:
In what cases Tkinter's grid() cannot be mixed with pack()?
i.e., having frames packed next to each other, but inside each frame, I have grids. It worked fine, until I upgraded Tcl/TK backend to 8.6.8 in official Python 3.7.2, when I start seeing interpreter woes:
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
I then switched everything to .pack()
, but now I have trouble to bring back my column designs where some widgets are wider than others.
Should I make everything .grid()
to achieve this or are there secret ingredients in .pack()
that I didn't see in docs?
Ideally I expect to achieve the following
- In a row of widgets, widgets are next to each other and fill the space as much as possible, similar to
.pack(side='left', fill='x', expand=True)
- In the same row, I can still specify one widget to be wider than the rest, so that when setting everything to
expand=True
, the relative sizes are still in control.
Example
On macOS, the following code gives me wider entry and narrower slider, while I'd like the other way around, with just .pack()
.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
mainframe = tk.Frame(root, bg='blue')
mainframe.pack()
row1 = tk.Frame(mainframe, bg='red')
entry = ttk.Entry(row1, text='Search...')
slider = ttk.Scale(row1)
btn1 = ttk.Button(row1, text='Reset')
btn2 = ttk.Button(row1, text='Help')
entry.pack(side='left', fill='x', expand=True)
slider.pack(side='left', fill='x', expand=True)
btn1.pack(side='left', fill='x', expand=True)
btn2.pack(side='left', fill='x', expand=True)
row1.pack()
root.mainloop()
I must stress that I know how to use .grid()
with my "rows" using .grid_columnconfigure(col, weight)
, which worked. It's just my working code (more complex than the above example, of course) simply broke after upgrading to TK 8.6, which implies that as my project gets bigger, mixed managers would be increasingly hard to maintain. So I decided to pick a camp between .pack()
and .grid()
. If one can achieve everything the other can at a cost, that'd be great.