I am programming a GUI using a Listbox as a file selection pane. Originally I was using <Double-1>
as my event trigger, but decided to switch to <Button-1>
. Long story short, I find there is a difference in how they process. <Button-1>
event requires 2 clicks for lb.curselection() to be accurate; whereas <Double-1>
works as expected.
Why does it do this and/or is this a bug in Tkinter? Thank you.
import tkinter as tk
class GUI:
def __init__(self):
self.root = tk.Tk()
self.lb = tk.Listbox(self.root)
self.lb.pack(side="left")
for i in range(10):
self.lb.insert("end", f"Value {i}")
self.lb.bind("<Double-1>", self.gui_dblClick_event)
def gui_dblClick_event(self, event):
print(self.lb.curselection())
def mainloop(self):
self.root.mainloop()
if __name__ == "__main__":
rootWin = GUI()
rootWin.mainloop()
<Double-1>
Output: imgur
- "Value 0" - (0,)
- "value 1" - (1,)
- "value 2" - (2,)
- "Value 3" - (3,)
<Button-1>
Output: imgur
- "Value 0" - ( )
- "Value 1" - (0,)
- "Value 2" - (1,)
- "Value 3" - (2,)
<Button-1>
Double Click is where this becomes very clear.
<Button-1>
Double Click Output: imgur
- "Value 0" - ( )
- "Value 0" - (0,)
- "Value 1" - (0,)
- "Value 1" - (1,)
- "Value 2" - (1,)
- "Value 2" - (2,)