1

I'm trying to create a GUI and was using Tkinter to create that GUI, I had one root with three different frames, one on the top, on the bottom left and one on the bottom right. The top hasn't been appearing and i was wondering if i did something wrong and how i could fix it. (I'm also very new to Tkinter and Python)

import tkinter as tk
from typing import Text
MainRoot= tk.Tk()
MainRoot.wm_geometry("800x500")
MainRoot.wm_title("Encode")



Encode_Frame = tk.Frame(MainRoot, bg="Gray", height=400, width=400)
Encode_Frame.pack(anchor="s",side=tk.LEFT)

Decode_Frame = tk.Frame(MainRoot, bg="Blue", height= 400, width=400)
Decode_Frame.pack(anchor="s",side=tk.RIGHT)

Text_Frame= tk.Frame(MainRoot,bg="Red",height=100,width=800)
Text_Frame.pack(side=tk.TOP, anchor= "n")



MainRoot.mainloop()
Ewok
  • 21
  • 3
  • Try manually resizing the window. That might give you a hint. – Bryan Oakley Feb 03 '22 at 22:45
  • Also, if you want a frame across the whole top, you should pack it first otherwise it will be between the left and right sides. – Bryan Oakley Feb 03 '22 at 22:46
  • For tkinter questions where something isn't laid out properly, it's helpful to draw diagrams of desired behaviour and include screenshots of actual behaviour. This is one of the relatively few good uses of images on Stack Overflow. – Karl Knechtel Feb 03 '22 at 23:16

1 Answers1

1

The root cause is that you're packing the left and right sides first. The way the packer works, when you pack a widget it takes up an entire side. So, once you pack something on the left, nothing can appear above or below it. Likewise when you pack something on the right.

When you pack the left and right first, the only space left is in the middle between the two sides. So, your third frame is going to the top _of the space between the two sides. Since the two sides are already filling the entire width of the window, there's no room left for the top frame.

The simple solution is to pack the top frame first.

Text_Frame.pack(side=tk.TOP, anchor= "n")
Encode_Frame.pack(anchor="s",side=tk.LEFT)
Decode_Frame.pack(anchor="s",side=tk.RIGHT)

For a comprehensive explanation of pack with images, see this answer to the question Tkinter pack method confusion

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685