Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter multiple selection Listbox

Tags:

python

tkinter

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

like image 849
Zenettii Avatar asked Jan 16 '13 15:01

Zenettii


2 Answers

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)

like image 110
Tharwen Avatar answered Oct 03 '22 10:10

Tharwen


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()]
like image 29
nanogoats Avatar answered Oct 03 '22 10:10

nanogoats