0

I am trying to create a basic layout for a Python gui using Tkinter.

#!/usr/bin/env python
from Tkinter import *
#create root
root = Tk()
banner = Frame(root, width=500)
body = Frame(root, width=500)
banner.grid()
body.grid()
bannercontent = Label(banner, bg="Green", text="Green").grid(row=0, column=0)
bannerbuttons = Label(banner, bg="Red", text="Red").grid(row=0, column=1)
bodycontent = Label(body, bg="Blue", text="Blue").grid(row=1, column=0, columnspan=2, sticky=E+W)
root.mainloop()

However with the above, the blue label is not expanding to fill the 2 columns as I'd wish, i.e. fill the space under Green and Red. What am I doing wrong please?

Example here https://i.sstatic.net/xUd10.jpg

example

Thank you in advance

La bla bla
8,72614 gold badges65 silver badges112 bronze badges
asked Apr 23, 2013 at 18:51

1 Answer 1

1

You have a few things wrong with your code. First, you aren't specifying a sticky option, so widgets won't "stick" (ie: grow or shrink) to the edges of the cell. Your bodycontent label is in fact occupying two columns, but the frame itself isn't expanding to the full width of the main window.

The second problem is that you seem to be assuming that columns span the entire app. They do not. You have two columns in banner because you put a widget in column 0 and column 1. However, bodycontent only has one column (or, you could say it has two, but the second column is of zero width). The column sizes in the first frame have no relationship to the columns in the second frame. Thus, the bottom frame contents won't line up with the top frame.

A third problem that your code has is that you aren't giving your rows or columns "weight". The weight attribute tells Tkinter how to allocate any extra space. Since you haven't done that, your GUI does not resize in a pleasant way when you grow or shrink the window. A good rule of thumb is that for any given container, at least one row and one column should be given a non-zero weight.

answered Apr 23, 2013 at 19:15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.