0

When I run the code below I do not get an error but the bottom frame doesn't appear in the window please can you tell me why and how I can make it appear (using the pack method NOT GRID please). I am using Python 3.5.0

import tkinter 
from tkinter import *
root = tkinter.Tk()
root.geometry("1920x1080")
TopFrame = Frame(root, width=1920, height=200, bg= "green")
TopFrame.pack(side=TOP)
MiddleRightFrame = Frame(root, width=1120, height=730, bg="orange")
MiddleRightFrame.pack(side=RIGHT)
MiddleLeftFrame = Frame(root, width=800, height=730, bg="black")
MiddleLeftFrame.pack(side=LEFT)
BottomFrame = Frame(root, width=1920, height=150, bg="blue")
BottomFrame.pack(side=BOTTOM)
7
  • There are a number of ways making your blue frame visible. Provide the image for the exact GUI you want. Commented Mar 3, 2018 at 18:24
  • You do realize that your frames will shrink or expand soon as they contain something else, right? Commented Mar 3, 2018 at 18:25
  • Yes I am going to make the buttons the exact size of the frame I will use 8 evenly sized buttons each height 150 and width 240 Commented Mar 3, 2018 at 18:29
  • I think you're forcing an overly direct way of producing a GUI, which needlessly complicates it all then. Where I think place excels over grid or pack. Commented Mar 3, 2018 at 18:31
  • 1
    When you say "make it appear" where do you want it to appear? The way you've coded it it will appear between the left and right frames if there's room. Do you want it to appear at the very bottom of the window, spanning the whole window? Commented Mar 3, 2018 at 21:21

3 Answers 3

1

Your MiddleLeftFrame is 800 pixels wide. Your MiddleRightFrame is 1120 pixels. 1120 + 800 = 1920. You're forcing the window to be only 1920 pixels wide, so there's no room for the blue frame.

Remove this line and your frame will appear: root.geometry("1920x1080")

If your intent is for it to appear at the bottom of the window, spanning the entire width of the window, then call pack on it before you call pack on the left and right sides.

Also, I strongly recommend grouping your pack statements together. It makes the code easier to manage in my experience (and I have a lot of experience!).

import tkinter 
from tkinter import *
root = tkinter.Tk()
root.geometry("1920x1080")

TopFrame = Frame(root, width=1920, height=200, bg= "green")
MiddleRightFrame = Frame(root, width=1120, height=730, bg="orange")
MiddleLeftFrame = Frame(root, width=800, height=730, bg="black")
BottomFrame = Frame(root, width=1920, height=150, bg="blue")

TopFrame.pack(side=TOP)
BottomFrame.pack(side=BOTTOM)
MiddleRightFrame.pack(side=RIGHT)
MiddleLeftFrame.pack(side=LEFT)

root.mainloop()

The reason this works is due to the packer algorithm. When you place something on the left or right, it will allocate all of the remaining vertical space on that side. Thus, after you pack something on the left and right and then later pack something on the bottom, the "bottom" is the bottom of the space between the left and right, not the bottom of the window as a whole.

Here is the canonical description of how pack works:

http://tcl.tk/man/tcl8.5/TkCmd/pack.htm#M26

Sign up to request clarification or add additional context in comments.

5 Comments

This is the only answer that addresses the why part of the question. Thanks for that. Does that mean when there's not enough space, the widgets are simply not shown?
This is incorrect (I know I could have been more clear in my question by adding an image however I could not get the image to work). The width of both frames is 1920 and their height is 730 meaning there is still a space of width 1920 and height 350 (150 for the blue frame and 200 for the green frame). TLDR: The placement of the frames fits in a 1920x1080 window. See the answer by @Carwley. That is the answer I was looking for.
@Nae: correct, it's possible for widgets to be invisible if there's not enough space for them, as this example shows.
@Fake: No, it's not incorrect. I can say with certainty. The way pack was used in this example, the frame is placed between the left and right side. Even though the frame in question was placed on the bottom, it's the bottom of the available space, not the bottom of the window as a whole. As I suggested, remove the call to geometry and the frame will appear between the left and right sides. You didn't ask how to get it to appear at the bottom of the window, you simply asked why it wasn't appearing.
@Fake: I've updated my answer to give a better explanation, and to show a very simple solution if your goal is for the bottom frame to be at the very bottom of the window.
0

I think the issue is that you are using pack sides so that in the middle there is a line of nothing. One way to get around this is to create a MiddleFrame where pack sides are used and then just pack the other frames.

import tkinter 
from tkinter import *

root = tkinter.Tk()
root.geometry("1920x1080")

TopFrame = Frame(root, width=1920, height=200, bg= "green")
TopFrame.pack()

#the middle frame
MiddleFrame = Frame(root)

#pack the two middle frames into the frame created above
#the parent of the two middle frames change to become MiddleFrame instead of root
MiddleRightFrame = Frame(MiddleFrame, width=1120, height=730, bg="orange")
MiddleRightFrame.pack(side=RIGHT)

MiddleLeftFrame = Frame(MiddleFrame, width=800, height=730, bg="black")
MiddleLeftFrame.pack(side=RIGHT)

#pack the middle frame with both frames inside it
MiddleFrame.pack()

BottomFrame = Frame(root, width=1920, height=150, bg="blue")
BottomFrame.pack()

Comments

0

Add:

tkinter.mainloop()

so that the GUI starts waiting for events as opposed to skipping to close itself.


Additionally, pack uses a filling algorithm which calculates dynamically to fill the empty space. You shouldn't really be doing it like this but a simple call swap would suffice in this specific case. Call:

BottomFrame.pack(side=BOTTOM)

exactly after TopFrame's pack:

TopFrame.pack(side=TOP)
BottomFrame.pack(side=BOTTOM)
MiddleRightFrame.pack(side=RIGHT)
MiddleLeftFrame.pack(side=LEFT)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.