I'm creating a Tkinter-based app. It has a Listbox widget, and I want to get selected item on the double-click. Everything works fine if I double-click on an item, but if I double-click on the free part of the Listbox, the last item is getting selected and curselection returns a tuple with it (instead of an empty tuple).
Here is the code:
from tkinter import *
root = Tk()
listbox = Listbox(root)
for item in ("foo", "bar"):
listbox.insert(END, item)
listbox.bind("<Double-1>", lambda event: print(listbox.curselection()))
listbox.pack()
root.mainloop()
I also tried using listbox.get(ACTIVE) and listbox.nearest(event.y) as @stovfl suggested but, unfortunately, it didn't help either. In the first case it returned the last string that was shown in the Listbox instead of the tuple with the last index. And in the second case it just returned the last index instead of the tuple with it.
PS: I have been here. But I don't bind the event to the root window, I bind it to the Listbox, but it doesn't work as I need either.
