I created a simple program with two lists, and select elements in them. I want to ensure that both lists have been allocated one item at a time. But if I selected an item in one list, then when I switch to another list, the focus from the selected item is removed.

Example:

def Get(event): print event.widget.curselection() from Tkinter import * r = Tk() l = Listbox(r) l1 = Listbox(r) l.insert(END, 'a', 'b', 'c', 'd', 'e') l1.insert(END, 'a', 'b', 'c', 'd', 'e') l.pack() l1.pack() l1.bind("<<ListboxSelect>>", Get) l.bind("<<ListboxSelect>>", Get) r.mainloop() 
  • With 100% certainty, I can’t argue, but most likely the reason is that under Windows two controls at the same time contain focus (as under Linux I can’t say). Maybe you really need Radiobutton? - insolor
  • I decided to replace the Listbox with the Combobox, because I basically needed three lists on the form. The result was two drop-down lists and a listbox :) - Ron Barhash

1 answer 1

By default, when moving to another list, the highlight of the current list row is deselected. To avoid this behavior and achieve the preservation of the backlight, you need to set the exportselection parameter to zero for an item of type ListBox :

 import tkinter root = tkinter.Tk() leftListBox = tkinter.Listbox(exportselection=0) leftListBox.insert(tkinter.END, "left 1", "left 2", "left 3") leftListBox.pack(side=tkinter.LEFT) middleListBox = tkinter.Listbox() middleListBox.insert(tkinter.END, "middle 1", "middle 2", "middle 3") middleListBox.pack(side=tkinter.LEFT) rightListBox = tkinter.Listbox() rightListBox.insert(tkinter.END, "right 1", "right 2", "right 3") rightListBox.pack(side=tkinter.LEFT) root.mainloop() 

At the same time there is one interesting subtlety. This parameter not only causes the list to keep the highlight when moving to another list, but also preserves the highlight of another list from which the transition was made to it. In the example above, the following behavior will be:

  • In the leftListBox highlighted line will always be highlighted.
  • After selecting the line in the middleListBox ( rightListBox ) and moving to the leftListBox line in the middleListBox ( rightListBox ) will remain highlighted.
  • After highlighting a row in the middleListBox and moving to the rightListBox row highlighting in the middleListBox will be removed.