如何对两个列表框使用单个滚动滑块
How to use a single scroll slider for two list boxes
我希望能够使用同一个滑块使两个列表框同时滚动。我已经设法让鼠标滚轮同时为两者工作,所以我现在只需要滑块工作。真正的列表最终将具有相同数量的内容,因此如果两者都连接到滚动滑块,它们应该没问题。我设法让滑块在一个但不是两个上工作。
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()
您可以将以下功能添加到您的代码中:
def my_yview(*args):
alphabetlist.yview(*args)
IDsearchresultslist.yview(*args)
设置滚动条配置如下:
scrollbar.config(command=my_yview)
这样,您可以使用一个垂直滚动条滚动两个列表。
例如:
...
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)
...
基于此tutorial。
我希望能够使用同一个滑块使两个列表框同时滚动。我已经设法让鼠标滚轮同时为两者工作,所以我现在只需要滑块工作。真正的列表最终将具有相同数量的内容,因此如果两者都连接到滚动滑块,它们应该没问题。我设法让滑块在一个但不是两个上工作。
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()
您可以将以下功能添加到您的代码中:
def my_yview(*args):
alphabetlist.yview(*args)
IDsearchresultslist.yview(*args)
设置滚动条配置如下:
scrollbar.config(command=my_yview)
这样,您可以使用一个垂直滚动条滚动两个列表。
例如:
...
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)
...
基于此tutorial。