I am using the following tkinter widgets: Entry
and Listbox
. I want the Entry widget to display a selected item in the Listbox The Listbox is configur
How to avoid tkinter
<
and> .curselection()
detecting events/selection outside of Listbox?
That depends on exactly what you mean. The <
event was explicitly designed to fire whenever the selection changes, no matter how it changes. That could mean when the user selects something new in the listbox, or when the selection is removed from the listbox.
The errors you get are because you assume that there is a selection, which may or may not be true. You need to check for a selection and only run your code if something is selected.
Another solution, or part of the overall solution, might be to set the exportselection
option of the listbox to False
. When set to True
-- the default -- the selection will be unset whenever any other widget gets the selection. When set to False
, the selection won't change just because another widget gets some or all of its data selected.