How to use a single scroll slider for two list boxes

I want to be able to make the two list boxes scroll at the same time with the same slider. I already managed to make the mouse wheel work for both at the same time so I just need the slider to work now. The real lists will end up having the same number of contents so if both get connected to the scroll slider, they should be fine. I managed to get the slider to work on one but not both.

from tkinter import *  # allows for the use of tkinter GUI

win = Tk()  # creates the tkinter window
win.title("test")  # sets the name of the window
win.geometry("1100x700")  # sets the size of the window
win.resizable(False, False)  # prevents the window from being resized


# these are made up lists for this question
databaselist2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
databaselist1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]


# -----------------------------------------------------------------------------------------------------------------------
# this synchronizes the mouse scrolled for both list boxes
def scroll2(event):  # sets up the scroll2 function
    IDsearchresultslist.yview_scroll(int(-4 * (event.delta / 120)), "units")  # makes it so that when the user scrolls
    # though one list box, all the list boxes scroll as well


def scroll1(event):  # sets up the scroll1 function
    alphabetlist.yview_scroll(int(-4 * (event.delta / 120)), "units")  # makes it so that when the user scrolls
    # though one list box, all the list boxes scroll as well


# -----------------------------------------------------------------------------------------------------------------------


# this section sets up the list boxes and the single scroll bar
alphabetframe = Frame(win)  # creates the frame that will contain the scrollbar for the alphabetframe
scrollbar = Scrollbar(alphabetframe, orient=VERTICAL)  # creats the scrollbar for the alphabetlist box
IDsearchresultslist = Listbox(win, width=39, font=('Arial', 12, 'bold'),
                              yscrollcommand=scrollbar.set)  # creates the
scrollbar.config(command=IDsearchresultslist.yview)  # configures the scrollbar to be able to see the y axis

# of the listbox
# ID search list box that will contain the

IDsearchresultslist.place(relx=.6, rely=.15)  # moves the ID search results box to its location

# this section creates the first buttons that will show up when the program is opened
alphabetlist = Listbox(alphabetframe, width=50, font=('Arial', 12, 'bold'),
                       yscrollcommand=scrollbar.set)  # creates the search entry box
scrollbar.config(command=alphabetlist.yview)  # configures the scrollbar to be able to see the y axis

# of the listbox
scrollbar.pack(side=RIGHT, fill=Y)  # makes the scrollbar apear on the right side of the alphabetlist box
alphabetframe.place(relx=.05, rely=.2)  # moves the frame that contains the scrollbar to its location

alphabetlist.pack(pady=15, padx=15)

alphabetlist.bind("<MouseWheel>", scroll2)  # when the mousewheel is used, the scroll2
# function with be activated
IDsearchresultslist.bind("<MouseWheel>", scroll1)  # when the mousewheel is used, the scroll1
# function with be activated


# ----------------------------------------------------------------------------------------------------------------------
# this adds all the information from both lists into box list boxes
alphabetlist.delete(0, END)  # clears the alphabetlist
for item in databaselist2:  # loops though the list
    alphabetlist.insert(END, item)  # moves each individual item from the list into the alphabet listbox
    # as the list gets looped

for item in databaselist1:  # loops though the list
    IDsearchresultslist.insert(END, item)  # moves each individual item from the list into the alphabet listbox
    # as the list gets looped

win.mainloop()

Answer

You can add the following function to your code:

def my_yview(*args):
    alphabetlist.yview(*args)
    IDsearchresultslist.yview(*args)

Set the scrollbar config as follows:

scrollbar.config(command=my_yview)

This way, you can use one vertical scrollbar to scroll through two lists.

For example:

...
def my_yview(*args):
    alphabetlist.yview(*args)
    IDsearchresultslist.yview(*args)

# this section sets up the list boxes and the single scroll bar
alphabetframe = Frame(win)  # creates the frame that will contain the scrollbar for the alphabetframe
scrollbar = Scrollbar(alphabetframe, orient=VERTICAL)  # creats the scrollbar for the alphabetlist box
IDsearchresultslist = Listbox(win, width=39, font=('Arial', 12, 'bold'),
                              yscrollcommand=scrollbar.set)  # creates the
# scrollbar.config(command=IDsearchresultslist.yview)  # configures the scrollbar to be able to see the y axis

# of the listbox
# ID search list box that will contain the

IDsearchresultslist.place(relx=.6, rely=.15)  # moves the ID search results box to its location

# this section creates the first buttons that will show up when the program is opened
alphabetlist = Listbox(alphabetframe, width=50, font=('Arial', 12, 'bold'),
                       yscrollcommand=scrollbar.set)  # creates the search entry box
# scrollbar.config(command=alphabetlist.yview)  # configures the scrollbar to be able to see the y axis

scrollbar.config(command=my_yview)
...

Based on this tutorial.