tkinter listboxes

R

rahulnag22

I will give a simplified example of the problem at hand --

I have a case in which I have two listboxes - listbox1 and listbox2,
if I click on an item in listbox1 the item gets highlighted as
expected. Now if I click on an item in listbox2 the selected item in
listbox1 loses its highlight. My question is how do I keep the
listbox1 item from losing its highlight if I select an item in
listbox2 or to that matter any other widget.

Thanks
Rahul
 
J

James Stroud

I will give a simplified example of the problem at hand --

I have a case in which I have two listboxes - listbox1 and listbox2,
if I click on an item in listbox1 the item gets highlighted as
expected. Now if I click on an item in listbox2 the selected item in
listbox1 loses its highlight. My question is how do I keep the
listbox1 item from losing its highlight if I select an item in
listbox2 or to that matter any other widget.

Thanks
Rahul

You will need to bind '<Button-1>' for each list box to something like:


def button1(e):
row = e.widget.lists[0].nearest(e.y)
e.widget.selection_clear(0, END)
e.widget.selection_set(row)
return 'break'

# how to bind
alistbox.bind('<Button-1>', button1)


The less than obvious thing here is that selections (highlighted here)
are different than the active index (which is marked with an underline).
You are probably using the active index and don't realize the difference.

James
 
E

Eric Brunel

I will give a simplified example of the problem at hand --

I have a case in which I have two listboxes - listbox1 and listbox2,
if I click on an item in listbox1 the item gets highlighted as
expected. Now if I click on an item in listbox2 the selected item in
listbox1 loses its highlight. My question is how do I keep the
listbox1 item from losing its highlight if I select an item in
listbox2 or to that matter any other widget.

By default, the 'highlighting' is considered as a selection. Since you
can't have two items selected at the same time, the second cancels the
first. To avoid this behaviour, make each list keep its selection to
itself by using the 'exportselection=0' option when you create the Listbox
instance. This should work as you expect.

HTH
 
R

rahulnag22

By default, the 'highlighting' is considered as a selection. Since you
can't have two items selected at the same time, the second cancels the
first. To avoid this behaviour, make each list keep its selection to
itself by using the 'exportselection=0' option when you create the Listbox
instance. This should work as you expect.

HTH


Eric that works. Thank You. James thank you for the detailed
explanation it will be useful for me.
Regards
Rahul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top