2

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

Bill
  • 1,241
  • 17
  • 25
  • Read about [Tkinter.Grid.grid_columnconfigure-method](http://effbot.org/tkinterbook/grid.htm#Tkinter.Grid.grid_columnconfigure-method) and [gui layout using frames and grid](https://stackoverflow.com/questions/34276663/tkinter-gui-layout-using-frames-and-grid/34277295#34277295) – stovfl Mar 01 '20 at 20:26
  • @stovfl tried that along with other potential solutions. Nothing that I did seemed to have the intended effect. Either I did so incorrectly, or that answer does not produce the correct result. If that is a known answer, then please provide a code sample and I will mark as answer. – Bill Mar 01 '20 at 20:56
  • Does this answer your question? [Python 3 Tkinter - Create Text Widget covering 100% Width with Grid](https://stackoverflow.com/questions/27614037/python-3-tkinter-create-text-widget-covering-100-width-with-grid) – stovfl Mar 01 '20 at 21:17

1 Answers1

5

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()
Scott
  • 367
  • 1
  • 9
  • 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 – Bill Mar 02 '20 at 13:08