How to avoid tkinter <> and .curselection() detecting events/selection outside of Listbox?

前端 未结 1 960
感动是毒
感动是毒 2021-01-23 01:35

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

相关标签:
1条回答
  • 2021-01-23 01:51

    How to avoid tkinter <<ListboxSelect>> and .curselection() detecting events/selection outside of Listbox?

    That depends on exactly what you mean. The <<ListboxSelect>> 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.

    0 讨论(0)
提交回复
热议问题