1

I have a really simple GUI program written in python with tkinter:

from Tkinter import *
from ttk import Combobox

class TestFrame(Frame):

    def __init__(self, root, vehicles):

        dropdownVals = ['test', 'reallylongstring', 'etc']
        listVals = ['yeaaaah', 'mhm mhm', 'untz untz untz', 'test']

        self.dropdown = Combobox(root, values=dropdownVals)
        self.dropdown.pack()

        listboxPanel = Frame(root)

        self.listbox = Listbox(listboxPanel, selectmode=MULTIPLE)        
        self.listbox.grid(row=0, column=0)

        for item in listVals:
            self.listbox.insert(END, item) # Add params to list

        self.scrollbar = Scrollbar(listboxPanel, orient=VERTICAL)
        self.listbox.config(yscrollcommand=self.scrollbar.set) # Connect list to scrollbar
        self.scrollbar.config(command=self.listbox.yview) # Connect scrollbar to list
        self.scrollbar.grid(row=0, column=1, sticky=N+S)

        listboxPanel.pack()

        self.b = Button(root, text='Show selected list values', command=self.print_scroll_selected)
        self.b.pack()

        root.update()

    def print_scroll_selected(self):

        listboxSel = map(int, self.listbox.curselection()) # Get selections in listbox

        print '=========='
        for sel in listboxSel:
            print self.listbox.get(sel)
        print '=========='
        print ''

# Create GUI
root = Tk()
win = TestFrame(root, None)
root.mainloop();

The GUI looks like this:

I click in a few items in the ListBox and hit the button. The output I get is as expected:

==========
untz untz untz
==========

==========
yeaaaah
untz untz untz
==========

I now choose a value from the ComboBox and suddenly the selection in the ListBox is gone. Hitting the button shows that no value is selected in the ListBox:

==========
==========

My question is: why does the selection of a ComboBox item clear the selection of the ListBox? They are in no way related so this bug really puzzles me!

Pphoenix
  • 1,423
  • 1
  • 15
  • 37

1 Answers1

3

I finally found the answer. I will leave this question here in case someone else finds it.

The problem was not in the ComboBox but in the ListBox. This becomes clear if I use two (unrelated) ListBoxes. With two ListBoxes the selection will clear when the focus is changed. From this question and its accepted answer I found that adding exportselection=0 to a ListBox disables the X selection mechanism where the selection is exported.

From effbot listbox about X selection mechanism: selects something in one listbox, and then selects something in another, the original selection disappears.

Community
  • 1
  • 1
Pphoenix
  • 1,423
  • 1
  • 15
  • 37