Returning the index of an item clicked in a tkinter Listbox
11,148
def fileSelection(self):
selection = listbox.curselection
print(selection)
Looks like you forgot the parentheses.
def fileSelection(self):
selection = listbox.curselection()
print(selection)
Author by
Volkisch
Updated on June 04, 2022Comments
-
Volkisch almost 2 years
I'm trying to get tkinter to return the index of an item clicked in listbox. Here's my code.
def fileSelection(self): selection = listbox.curselection print(selection) listbox.bind("<Button-1>", fileSelection)
Right now it prints
bound method Listbox.curselection of tkinter.Listbox object at 0x00320E30
no matter what item is clicked on. If I change the code to include a button like this:
button = Button(text=u"test", command=OnButtonClick) def OnButtonClick(): selection = listbox.curselection() print(selection)
and select the Listbox item, then click the button, it will print the index of the selected item, as expected, but that's an extra step I don't want.
-
Volkisch almost 9 yearsChrist... can I delete this? slinks away
-
Russell Smith almost 9 yearsThat effbot page is waaaayyyy out of date. The GUI will fire an event when the selection changes (
<<ListobxSelect>>
) Regardless, your solution doesn't answer the actual question that was asked, it just works around it.