Code Guy
Code Guy

Reputation: 61

Tkinter scrolling listboxes

I am working on a project with my friend. We are using 16 listboxes. We made a progam to scroll all of them at the same time, but the problem is that it took us around 300 lines of code and we want to tidy it up but we dont know how to do it. We made a little prototype, but we cant figure out how to stop listbox from scrolling more than other because of hovering over it. Can someone tell us how to do that?

from tkinter import *

screen = Tk()
screen.title("EEPROM programmer")
screen.geometry("1050x620")
screen.maxsize(width = 1050, height = 620)
screen.minsize(width = 1050, height = 620)
listBoxes = {}
listNumber = 16
scrollbar = Scrollbar(screen)
scrollbar.place(x=25*17,y=25,height=100)

scrollPosition = 0

print(len(listBoxes))

def scrolllist(event):
    global scrollPosition
    scrollPosition += (((-event.delta)/120)*7)/75
    if scrollPosition >= 0.75:
        scrollPosition = 0.75
    elif scrollPosition <= 0:
        scrollPosition = 0
    print(scrollPosition)
    for num in range(listNumber):
        listBoxes[num].yview("moveto", scrollPosition)
    print()

for x in range(listNumber):
    listBoxes[x] = Listbox(screen, font=("consolas", 12, "bold"), height=25, width=3, yscrollcommand=scrollbar.set)
    listBoxes[x].place(x=25*x, y=25)
    listBoxes[x].bind("<MouseWheel>", scrolllist)

for y in range(100):
    for x in range(listNumber):
        listBoxes[x].insert(0, y)

def scrollLists(x, y):
    print(x)
    print(y)
    global scrollPosition
    scrollPosition = float(y)
    for num in range(listNumber):
        listBoxes[num].yview(x, y)

scrollbar.config(command = scrollLists)

screen.mainloop()

Upvotes: 0

Views: 82

Answers (2)

Axe319
Axe319

Reputation: 4365

2 things. First bind all of your listboxes on the screen together rather than individually.

screen.bind_all("<MouseWheel>", scrolllist)

Secondly put in an offset for the movement from the hover like so.

listBoxes[num].yview("scroll", event.delta, "units")

I also condensed your scrollLists function since that was taking the wrong amount of arguments.

Full code.

from tkinter import *

screen = Tk()
screen.title("EEPROM programmer")
screen.geometry("1050x620")
screen.maxsize(width = 1050, height = 620)
screen.minsize(width = 1050, height = 620)
listBoxes = {}
listNumber = 16
scrollbar = Scrollbar(screen)
scrollbar.place(x=25*17,y=25,height=100)

scrollPosition = 0

print(len(listBoxes))

def scrolllist(event):
    global scrollPosition
    scrollPosition += (((-event.delta)/120)*7)/75
    if scrollPosition >= 0.75:
        scrollPosition = 0.75
    elif scrollPosition <= 0:
        scrollPosition = 0
    print(scrollPosition)
    for num in range(listNumber):
        listBoxes[num].yview("scroll", event.delta, "units")
        listBoxes[num].yview("moveto", scrollPosition)
    print()

for x in range(listNumber):
    listBoxes[x] = Listbox(screen, font=("consolas", 12, "bold"), height=25, width=3, yscrollcommand=scrollbar.set)
    listBoxes[x].place(x=25*x, y=25)
screen.bind_all("<MouseWheel>", scrolllist)

for y in range(100):
    for x in range(listNumber):
        listBoxes[x].insert(0, y)

def scrollLists(*args):
    for num in range(listNumber):
        listBoxes[num].yview(*args)

scrollbar.config(command = scrollLists)

screen.mainloop()

Upvotes: 2

Joseph Arnold
Joseph Arnold

Reputation: 11

You could try doing something like this. This will auto adjust for how ever long the contents are within the listbox.

scroll = Scrollbar(screen)
scroll.pack(side=RIGHT, fill=Y)
listbox = Listbox(screen, yscrollcommand=scroll.set)
listbox.pack()
scroll.config(command=info.yview)

Upvotes: 0

Related Questions