views:

671

answers:

4
from Tkinter import *

master = Tk()

listbox = Listbox(master)
listbox.pack()

listbox.insert(END, "a list entry")

for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)

listbox2 = Listbox(master)
listbox2.pack()

listbox2.insert(END, "a list entry")

for item in ["one", "two", "three", "four"]:
    listbox2.insert(END, item)

mainloop()

The code above creates a tkinter window with two listboxes. But there's a problem if you want to retrieve the values from both, because as soon as you select a value in one, it deselects whatever you selected in the other. Is this just a limitation developers have to live with?

A: 

exportselection=0 when defining a listbox seems to take care of this issue

directedition
+2  A: 

From the documentation:

By default, the selection is exported to the X selection mechanism. If you have more than one listbox on the screen, this really messes things up for the poor user. If he selects something in one listbox, and then selects something in another, the original selection is cleared. It is usually a good idea to disable this mechanism in such cases. In the following example, three listboxes are used in the same dialog:

b1 = Listbox(exportselection=0)
for item in families:
    b1.insert(END, item)
b2 = Listbox(exportselection=0)
for item in fonts:
    b2.insert(END, item)
b3 = Listbox(exportselection=0)
for item in styles:
    b3.insert(END, item)
jcoon
A: 

Thanks!!! You help me a lot :D:D:D

Esteban