【发布时间】:2021-10-17 02:38:02
【问题描述】:
我希望能够使用相同的滑块使两个列表框同时滚动。我已经设法让鼠标滚轮同时为两者工作,所以我现在只需要滑块工作。真正的列表最终将具有相同数量的内容,因此如果两者都连接到滚动滑块,它们应该没问题。我设法让滑块在其中一个上工作,但不是同时在两个上工作。
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()
【问题讨论】:
-
第一个是很没用的cmets,大部分都可以直接删掉,不需要解释代码是做什么的,从代码上应该可以理解,需要说明原因和方法它确实或某事,基本上不要像
now we create a function那样写sth,这本身就很清楚。其次不要全部导入,导入时不要使用*,只导入你需要的,比如from tkinter import Tk, Button, Label, Frame等等,不要告诉我你不知道你需要什么,什么时候需要,添加到导入(我对导入非常认真) -
滑块是指滚动条吗?
标签: python tkinter listbox scrollbar