3

I am learning Python GUI programming with tkinter. I wanted to place a frame in my root window using the grid geometry manager, specify a height, and have the frame expand to the full width of the root window. I tried to do this using the sticky options but this does not produce the desired result. How do I make the frame expand to the full width of the window without manually specifying the width?

Code:

import tkinter
import tkinter.ttk

win = tkinter.Tk()
win.geometry('600x600')

frame = tkinter.Frame(win, height=300)
frame.configure(bg='red')
frame.grid(column=0, sticky=tkinter.E + tkinter.W)

win.mainloop()

enter image description here

3

1 Answer 1

6

I believe this code will achieve the result you are looking for (note that call to grid_columnconfigure is on win, which is the parent of your frame widget):

import tkinter
import tkinter.ttk

win = tkinter.Tk()
win.geometry('600x600')

frame = tkinter.Frame(win, bg='red', height=300)
frame.grid(row=0, column=0, sticky='ew')
win.grid_columnconfigure(0,weight=1)

win.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Scott. I tried to use columnconfigure, but made the mistake that you pointed out which is that the call must be on the root container not the child container. This worked pefectly

Your Answer

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.