Luke__
Luke__

Reputation: 247

Can't align three text widget in tkinter

I'd like to create three text areas in a tkinter window and make them dinamically resizable. I thought that one solution was to pass the width and height parameters in pixels (such as height=int(win_height/2)), but as I read it isn't possible, in fact the width and height parameters in a tk.Text widget are calculated by characters for each line and column. I've also tried to pass the width and height parameters in percentages (such as height=50%) but it returns me a syntax error.

I've been trying to find out a solution for this problem in the net, and the best code I've found is this:

import tkinter as tk

root = tk.Tk()
root.geometry("500x500")

# Text Box
first_textbox = tk.Text(root, width=25, height=10, bg='yellow')
second_textbox = tk.Text(root, width=25, height=10, bg='blue')
third_textbox = tk.Text(root, width=50, height=20, bg='red')

# Packing
first_textbox.grid(column=1, row=1)  
second_textbox.grid(column=1, row=2)
third_textbox.grid(column=2, row=1, rowspan=2)

root.mainloop()

By running this code I obtain a window with three different text areas which aren't dinamically resizabled and which take more space than the actual window width. I hope you can help me. Sorry for any English mistake, it is my second lenguage

Upvotes: 0

Views: 153

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386335

grid has several documented parameters to help you do what you want. You simply need to use them.

By default, grid won't give widgets any extra space -- they take up only the space they need and no more. If you widgets to be allocated extra space, you have to explicitly arrange for that.

For example, if you want all widgets to grow and shrink equally, you need to configure the rows and columns to have an equal weight greater than zero. That will tell grid how to allocate any extra space when the window is bigger than the size requested by all of the widgets.

For example:

root.grid_rowconfigure((1,2), weight=1)
root.grid_columnconfigure((1,2), weight=1)

That just tells grid what to do with extra space. If instead, you want two or more rows or columns to have exactly the same size, you can use the uniform option to tell grid that you want the rows or columns to have a uniform (identical) size.

For example, if you want both columns 1 and 2 to have the same width, you can give each column the same value for the uniform option. Note: the value passed to uniform can be anything you want. The important thing is that they are configured to have the same value.

root.grid_columnconfigure((1, 2), uniform="equal")

That alone won't solve the problem. You also must tell grid that you want the widgets to fill the space given to them. You do that with the sticky parameter, which tells grid to "stick" the widget to one or more sides of the allocated space.

To get the widgets to fill all allocated space you can give the string "nsew" which stands for "north, south, east, and west" which represent the four sides of the given space.

first_textbox = tk.Text(root, width=25, height=10, bg='yellow')
second_textbox = tk.Text(root, width=25, height=10, bg='blue')
third_textbox = tk.Text(root, width=50, height=20, bg='red')

Upvotes: 0

Related Questions