T. Anserson T. Anserson - 4 months ago 31
Python Question

How to Update Scrollbar Position with Tkinter

I am trying to create a GUI for a python script using Tkinter, and have a working scrollbar. However, the position of the 'bar' does not update when I scroll or drag the bar. I think the relevant portion of code is as follows:

#Setup window with text and scrollbar
root = Tk()
scrollbar = Scrollbar(root)
app = App(root)
t = Text(root)

#GRID manager layout
t.grid(row = 0, column = 1, sticky=N+S+W, padx = 5, pady = 5)
scrollbar.grid(row = 0, column = 2, sticky=N+S+W, )

scrollbar.config( command = t.yview )


I have tried searching for means to fix this, but cannot seem to figure out what I'm doing wrong. Any help would be really appreciated. My apologies if I've not included enough code, if you would like more, or to see the entire script (though it's 100 lines) I would be happy to oblige.

Thanks again for your time.

Answer

You should point it to the .yview of a Canvas, and put the Text into the canvas

the_window = Tk()

vscrollbar = Scrollbar(the_window)
vscrollbar.grid(...)

the_canvas = Canvas(
    the_window,
    background = 'white',
    yscrollcommand = vscrollbar.set
)
the_canvas.grid(...)

vscrollbar.config(command=the_canvas.yview)