user1904898
user1904898

Reputation: 79

tkinter.scrolledtext.ScrolledText columnspan issue

Below is a section of code to help show what I want. The ScrolledText widget takes over 3/4 of the width but I have it setup to take up 1/2, trying to determine how to split two controls down the middle. When I comment out the ScrolledText widget and add a 2nd test listbox in it's place with same grid information it splits down the middle. Is there something different to setting up a ScrolledText widget?

page.rowconfigure(1, weight=1)
page.columnconfigure(0, weight=1)
page.columnconfigure(6, weight=1)

lb_reports = Listbox(page, selectmode=SINGLE)
lb_reports.grid(row=1, column=0, columnspan=5, sticky=W + N + S + E, pady=5, padx=5)

# Following list box is here as a test, it does split down the middle of screen as expected.
# self.lb_reports1 = Listbox(page, selectmode=SINGLE)
# self.lb_reports1.grid(row=1, column=5, columnspan=5, sticky=W + N + S + E, pady=5, padx=5)

# this widget takes right 3/4 of screen instead of 1/2
st_test = tkinter.scrolledtext.ScrolledText(page)
st_test.grid(row=1, column=6, columnspan=5, sticky=W + N + S + E, pady=5, padx=5)

Upvotes: 2

Views: 990

Answers (1)

fhdrsdg
fhdrsdg

Reputation: 10532

Giving columns a weight does not force the columns to have an equal size, but forces them to grow at the same rate. From effbot:

weight=
A relative weight used to distribute additional space between columns. A column with the weight 2 will grow twice as fast as a column with weight 1. The default is 0, which means that the column will not grow at all.

Then from the documentation of the Text widget (off of which the ScrolledText is based) you can see that the default width of the widget is 80 (characters, not pixels!), while the Listbox has a default width of 20 (again, characters).

So while both columns grow at the same rate, they do not start off at the same size.

Now if you give both widgets the same width value, you might see that they are still not the same width. This can happen because the fonts for the Text and Listbox widgets are not the same. If you give them both the same width and the same font, they should have the same width to start off with, so they will stay the same width when they grow:

from tkinter import *
from tkinter.scrolledtext import *

page = Tk()
page.rowconfigure(1, weight=1)
page.columnconfigure(0, weight=1)
page.columnconfigure(6, weight=1)

lb_reports = Listbox(page, selectmode=SINGLE, font=('Arial', 12), width=20)
lb_reports.grid(row=1, column=0, columnspan=5, sticky=W + N + S + E, pady=5, padx=5)

st_test = ScrolledText(page, font=('Arial', 12), width=20)
st_test.grid(row=1, column=6, columnspan=5, sticky=W + N + S + E, pady=5, padx=5)

page.mainloop()

Upvotes: 2

Related Questions