Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand Text widget to fill the entire parent Frame in Tkinter

Tags:

python

tkinter

tk

I got this Text widget, and I'd like for it to expand and fill its entire parent, using the Grid geometry manager.

According to the examples I've seen, this sample program should work, alas it doesn't, when expanding the window, the contents are not resizing.

from Tkinter import *
root = Tk()

input_text_area = Text(root)
input_text_area.grid(row=1, column=0, columnspan=4, sticky=W+E)
input_text_area.configure(background='#4D4D4D')

root.mainloop()

Any help is appreciated

For what it's worth, I'm running in Python 2.7 (latest 2.x version), and coding in PyCharm, though I don't think the IDE is relevant.

like image 860
So Many Goblins Avatar asked Feb 09 '15 21:02

So Many Goblins


1 Answers

When using grid, any extra space in the parent is allocated proportionate to the "weight" of a row and/or a column (ie: a column with a weight of 2 gets twice as much of the space as one with a weight of 1). By default, rows and columns have a weight of 0 (zero), meaning no extra space is given to them.

You need to give the column that the widget is in a non-zero weight, so that any extra space when the window grows is allocated to that column.

root.grid_columnconfigure(0, weight=1)

You'll also need to specify a weight for the row, and a sticky value of N+S+E+W if you want it to grow in all directions.

like image 178
Bryan Oakley Avatar answered Sep 21 '22 07:09

Bryan Oakley