# creating the 2 listboxes
possible = customtkinter.CTkLabel(master=root, text="Possible Reagents", font=font2, width=110)
possible.place(x=400, y=8)
possibleListbox = tkinter.Listbox(root, selectmode=tkinter.SINGLE, exportselection=False, width=15, height=10)
possibleListbox.place(x=400,y=30)
possibleScrollbar = tkinter.Scrollbar(root)
possibleScrollbar.place(x=493, y=30, height=164)
possibleListbox.bind("<<ListboxSelect>>", possibleClicked)
possibleListbox.bind("<Double-Button-1>", possibleClicked)
possibleListbox.config(yscrollcommand=possibleScrollbar.set)
possibleScrollbar.config(command=possibleListbox.yview)
selected = customtkinter.CTkLabel(master=root, text="Selected Reagents", font=font2, width=110)
selected.place(x=530, y=8)
selectedListbox = tkinter.Listbox(root, selectmode=tkinter.SINGLE, exportselection=False, width=15, height=10)
selectedListbox.place(x=530, y=30)
selectedScrollbar = tkinter.Scrollbar(root)
selectedScrollbar.place(x=623, y=30, height=164)
selectedListbox.bind("<<ListboxSelect>>", selectedClicked)
selectedListbox.bind("<Double-Button-1>", selectedClicked)
selectedListbox.config(yscrollcommand=selectedScrollbar.set)
selectedScrollbar.config(command=selectedListbox.yview)
#functions for moving one element to the other listbox
def possibleClicked(stuff):
global curselect
if possibleListbox.curselection():
curselect = possibleListbox.curselection()
selectedItem = possibleListbox.get(curselect)
possibleListbox.delete(curselect)
selectedListbox.insert(tkinter.END, selectedItem)
def selectedClicked(stuff):
global curselect
if selectedListbox.curselection():
curselect = selectedListbox.curselection()
selectedItem = selectedListbox.get(curselect)
selectedListbox.delete(curselect)
possibleListbox.insert(tkinter.END, selectedItem)
出于某种原因,我能够在两个列表框中选择空元素,即使我没有插入它们? 不确定是什么导致了这个错误,我试过检查所选元素是否为 == "",但这会导致另一个错误。
请参阅下面的 gif 以获得更好的解释
Listbox.curselection()
是当前选中项的list。要检索第一个选定的项目,您必须使用 curselect = possibleListbox.curselection()[0]
.