Skip to content
Advertisement

Python Tkinter multiple selection Listbox

I have a listbox which is setup with selection='multiple'. I then try to obtain a list of all the options the user has selected, by the code name.get(ACTIVE). the problem is that it doesn’t not always get all the options I have highlighted in the listbox GUI.

If I highlight one, it brings this back correctly.
If I highlight two or more (by a single click on each) it only returns the last item I selected
If I have multiple highlighted, but then click to un-highlight one, it’s this last one I clicked that gets returned even though it unhighlighted.

I’m expecting for the code to bring back what ever is highlighted.

The code to setup the listbox is:

self.rightBT3 = Listbox(Frame1,selectmode='multiple',exportselection=0)

The code to retreive the selections is:

selection = self.rightBT3.get(ACTIVE)

This is a screenshot of what the Application looks like in action, at the top you can see the console only registered the one selection (last one I clicked).

enter image description here

Advertisement

Answer

It seems the correct way to get a list of selected items in a Tkinter listbox is to use self.rightBT3.curselection(), which returns a tuple containing the zero-based index of the selected lines. You can then get() each line using those indices.

(I haven’t actually tested this though)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement