Holly
Holly

Reputation: 65

How can I make a text widget in tkinter that fills its grid position?

I am new to using grid_rowconfigure() and grid_columnconfigure() but from my understanding, the code below should make a centered textbox that has space either side:

self.grid_rowconfigure((0, 1, 2), weight=1)
self.grid_columnconfigure((0, 1, 2), weight=1)

Box = tk.Text(self, height=2, pady=4)
Box.grid(row=1, column=1)

However, the Text widget is the default width. How can I make it so it fills the column width (based on weight)?

I thought about placing a frame in the grid then packing the textbox within the frame to fill it but it looks the same as this.

UPDATE:

I used sticky="nsew" and that seems to work for the vertical height but not the horizontal height.

Fix: added a value for width in Box

Working Code:

self.grid_rowconfigure((0, 1, 2), weight=1)
self.grid_columnconfigure((0, 1, 2), weight=1)

Box = tk.Text(self, height=2, width=2, pady=4)
Box.grid(row=1, column=1, sticky="nsew")

Upvotes: 0

Views: 1482

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386295

You need to use the sticky attribute to cause a widget to fill the space given to it.

Box.grid(row=1, column=1, sticky=“nsew”)

Upvotes: 2

Related Questions