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).
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)
To get a list of text items selected in a listbox, I find the below solution to be the most elegant:
selected_text_list = [listbox.get(i) for i in listbox.curselection()]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With