Glenn
Glenn

Reputation: 315

Python Tkinter ttk Scrollbar on Text widget not scaling

Just learning tkinter and finding some odd behavior. My vertical scrollbar is light gray and contains the dark gray rectangle that travels up and down the bar to show progress, When this code first executes, there is no dark gray progress rectangle. When I press the down arrow of the progress bar, the dark gray box appears but stays at the top of the scrollbar until I scroll all the way to the bottom of the text. Only then does the scrollbar seem to determine the amount of text in the box, the dark gray progress rectangle jumps to the bottom of the scrollbar and works correctly from there on. I am on a Windows machine and executing this in a Jupyter Notebook using python 3.

Is there some way to initialize the scroll bar so it knows how much text it is dealing with?

 from tkinter import *
 from tkinter import ttk

 root = Tk()
 root.title('Twitter Timeline')

 f = ttk.Frame(root, padding = '3 3 12 12')
 f.grid(column=0, row=0, sticky=(N,W,E,S))
 root.columnconfigure(0, weight=1)
 root.rowconfigure(0, weight=1)

 t = Text(f, width= 100,  wrap='word')
 t.grid(column = 0, row = 2, sticky="nsew")
 t.insert(INSERT, "Begin " + "This is a test " * 800 + " END")

 s = ttk.Scrollbar(root, orient=VERTICAL, command=t.yview)
 s.grid(column=1, row=0, sticky='nsew')
 t['yscrollcommand'] = s.set

 for child in f.winfo_children(): 
     child.grid_configure(padx=5, pady=5)

 root.mainloop()

Upvotes: 0

Views: 301

Answers (1)

figbeam
figbeam

Reputation: 7176

Boy, this was a difficult one. Turns out the scrollbar reacts weird when your text consists of just one very long line. Try inserting a text with linebreaks:

t.insert(INSERT, "Begin " + "This is a test \n" * 800 + " END")

and it will work like a charm.

It looks like this is an issue with grid() as it works fine if you use pack() instead, also with the one long line... I'm not sure why.

Upvotes: 1

Related Questions