简体   繁体   中英

Why does <<ListboxSelect>> binding in Python's Tkinter trigger spuriously when double clicking an Entry widget?

As part of a larger bock of code, I discovered an oddity with tkinter listbox bindings. The below code gives a breakdown of the issue. When binding a listbox with 'listboxselected', selecting an item within that listbox, then double left clicking on the Entry, the listboxselected event is triggered once.

I have tested this in python 3.3.2, 3.6.3 and 3.7.1. The error only occurs in the two latter versions. I have checked the doc's and cant find any updates to cause this issue. Does anyone have any idea to why this behaviour may be occurring?

from tkinter import Tk, Listbox, Entry, Button
from functools import partial

root = Tk()

myList = Listbox(root)
myList.insert("end", "spam", "eggs", "chips")
myList.pack()
myList.bind("<<ListboxSelect>>", partial(print))

myEntry = Entry(root)
myEntry.pack()

I can't duplicate this on OSX, but my guess is that when you double-click in the entry, the text in the entry is selected. When the text is selected (even if there is nothing to select), the item in the listbox is de-selected. The de-selection triggers the binding.

If you want to be able to have something in both the listbox and entry selected, set exportselection=False in the listbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM