Loon
Loon

Reputation: 3

Manual Scroll with Tkinter leaving last element

I've written this piece of code with intention of making something similar to Excel, where you can scroll up and down in a spreadsheet. I have found, however, that when you scroll back up, that it leaves the lowest cell instead of scrolling it. I can't seem to figure out where I went wrong, any help is appreciated.

from tkinter import *

def scroll(event): #changes from what element to start drawing

    global starting_value
    starting_value -= event.delta/120

    draw_list(starting_value)

def draw_list(starting_value): #draws entries from specified index
    for index,i in enumerate(list_of_numbers[starting_value:starting_value+10]):
        i.grid(row=index)

list_of_numbers = [Entry() for i in range(100)]

for index,i in enumerate(list_of_numbers):
    i.insert(0,index)
    i.bind('<MouseWheel>',scroll)

starting_value = 0

draw_list(starting_value)

mainloop()

Upvotes: 0

Views: 35

Answers (1)

jasonharper
jasonharper

Reputation: 9597

Every time you scroll, any items that are no longer supposed to be visible are left in their original grid positions: you have perhaps placed new items at the same row/column, but that doesn't actually remove the original items from the grid. You'd need something like this at the top of draw_list() to explicitly un-grid the old items:

for widget in container.grid_slaves():
    widget.grid_forget()

Where container is the window or Frame containing your scrolling items - your example code does not actually give this object a name, you would need to do something like container = Tk() so that you can refer to it.

Upvotes: 1

Related Questions