Search code examples
pythontkinterttkwidgets

python tkinter listbox performance with other tk/ttk widgets


I tried to isolate the problem as best as i could.

Lets assume that there are 3 tk/ttk widgets. Why doesn't the tk_spinbox unselect the selected listbox entry and why does the ttk_spinbox? I dont want to unselect the items whenever the ttk_spinbox is pressed. Is there an workaround in order to get an equal behaviour for ttk_spinbox and tk_spinbox?

Here is the code:

import tkinter as tk
import tkinter.ttk as ttk

masterframe = tk.Tk()

listbox = tk.Listbox(masterframe, height=5, selectmode='multiple')
listbox.pack(padx=10, pady=10)
listbox.insert(tk.END, 'blubb_1')
listbox.insert(tk.END, 'blubb_2')

tk_spinbox = tk.Spinbox(masterframe,from_=10, to=20, increment=2)
tk_spinbox.pack(padx=10, pady=10)

ttk_spinbox = ttk.Spinbox(masterframe,from_=10, to=20, increment=2)
ttk_spinbox.pack(padx=10, pady=10)

masterframe.mainloop()

Solution

  • The "why" is simply, that's how they are designed to work. When you interact with a ttk spinbox, the spinbox value is automatically selected. This doesn't happen for the tk spinbox. By default only one thing can have the selection at a time, so the listbox loses the selection when the spinbox gains the selection.

    If you don't want to have the listbox lose the selection you can set the exportselection option to False on the listbox and/or the ttk spinbox.