pstatix
pstatix

Reputation: 3848

tkinter.Text widget exceeds grid manager constraints

TL;DR:

Text widget expands further than a specified column, row combo of a grid() call. Only resolution comes from changing the .width and .height attributes to something small and forcing the sticky flag of the call to expand and fill using 'nsew'.

Background:

The Text widget has sparked some confusion for me, take for example the following:

root = Tk()
root.geometry('400x400')
root.grid_columnconfigure(0, weight = 1)
root.grid_columnconfigure(1, weight = 1)
root.grid_rowconfigure(0, weight = 1)
root.grid_rowconfigure(1, weight = 1)
b = Button(root)
b.grid(column = 0, row = 0, sticky = 'nsew')
t = Text(root)
t.grid(column = 1, row = 1, sticky = 'nsew')

Given this, I should have a 400x400 Window like:

+-------+-------+
|       |       |
|   b   |       |
|       |       |
+-------+-------+
|       |       |
|       |   t   |
|       |       |
+-------+-------+

But what ends up happening is that the Text widget goes well beyond the lower right column, row:

+---+-----------+
| b |           |
+---+           |
|   |     t     |
|   |           |
|   |           |
|   |           |
+---+-----------+

The only way to resolve this is to alter the Text widget like so:

t = Text(root, height = 1, width = 1)

Then the sticky call expands the widget to only fit within the column, row of the grid. This however, seems like an extra step I shouldn't have to take even though the Text widget defaults to a .width and .height like all the other widgets.

Question: Why does the Text widget expand beyond the specified column, row of a grid?

Upvotes: 2

Views: 1458

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

Question: Why does the Text widget expand beyond the specified column, row of a grid?

It doesn't; it's the other way around: the rows and columns expand to fit the text widget. This isn't unique to the text widget. Tkinter will try to arrange things so that any widget will be displayed in its requested size if possible. In the case of a text widget, the default size is 80 characters by 24 characters, so your row and column expand to try to fit that widget.

This can be fairly easily seen if you add something in the third column and third row. You'll see that the second row is very tall and the second column is very wide. ie: the row and column expands to fit the text widget, rather than the text widget going over the edge of the row and column.

Add the following to your example code:

bottom_edge = Frame(root, background="red", height=4)
right_edge = Frame(root, background="red", width=4)
bottom_edge.grid(row=2, column=0, columnspan=3, sticky="ew")
right_edge.grid(row=0, column=2, rowspan=3, sticky="ns")

You'll have to resize the window since you made the window too small to hold everything. Once you do, you'll see a red border on the right and below, showing that it is the row and column that is expanding, rather than the text widget expanding beyond the row and column. The text widget is within the bounds of the second row, second column.

enter image description here

Upvotes: 2

Related Questions