0

I am faced with the problem to center side-stacked frames in a parent frame. I know how to center a single frame in a frame but I did not find a simple way to do this for several of them.

I get the following window

enter image description here

from the code below:

import Tkinter as tk

root = tk.Tk()
root.geometry("200x200")

# main frame
f = tk.Frame(root, background='black')
f.pack(expand=True, fill="both")

# two side-by-side frames inside, they fill up their space
f1 = tk.Frame(f, background='green')
f1.pack(side=tk.LEFT, expand=True, fill="both")
f2 = tk.Frame(f, background='red')
f2.pack(side=tk.LEFT, expand=True, fill="both")

# three fixed-size frames in the left frame above; I would like them to be centered in the frame
tk.Frame(f1, width=20, height=20,  background="orange").pack(side=tk.LEFT, fill=None, expand=False)
tk.Frame(f1, width=20, height=20, background="white").pack(side=tk.LEFT, fill=None, expand=False)
tk.Frame(f1, width=20, height=20, background="gray50").pack(side=tk.LEFT, fill=None, expand=False)

root.mainloop()

I would like the three square frames to be centered in the green one. I had to use tk.LEFT to position them, otherwise they would have been stacked up by default.

In my complete program, the green frame is there to exclusively contain the three square frames.

What is the most standard way to center the three square frames in the green one?

Community
  • 1
  • 1
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 1
    You can always put three frames in one new frame and you know how to center one frame :) – furas Jul 22 '14 at 12:00

1 Answers1

0

While thinking about furas's comment I realized that I did not understand the true difference between expand and fill (it is still a bit vague). It is possible to center the three frames by changing the f1.pack() line to:

f1.pack(side=tk.LEFT, expand=True, fill=None)

The f1 frame is tight around the three square (fill=None) ones buts tries to take as much space as possible in all directions (expand=True), effectively being centered. Note that the green background is not visible, the frame being tight around its content.

enter image description here

Community
  • 1
  • 1
WoJ
  • 27,165
  • 48
  • 180
  • 345