0

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,)
Tom Smith
  • 1
  • 1
  • The selection is done in the default handler which is executed after `` callback. – acw1668 Aug 30 '23 at 08:24
  • As acw1668 mentions, this probably has to do with the event order, check [this Q&A](https://stackoverflow.com/questions/2458026/python-gui-events-out-of-order/2472992#2472992) for more information. – fhdrsdg Aug 30 '23 at 08:32
  • @acw1668 Indeed. Due to this, even on Button-1 events, shouldn't the curselection() be a proper index value for the current selection? I have to Double Click a Single Click event to get an accurate index position. – Tom Smith Aug 30 '23 at 08:32
  • 1
    You can bind on the virtual event `<>` instead of ``. – acw1668 Aug 30 '23 at 08:34
  • @fhdrsdg Thank you. That is a good reference to have. Book marked for sure. – Tom Smith Aug 30 '23 at 08:36
  • @acw1668 I have continued experimenting with <> and bindtags. I appreciate your suggestion. However, I don't believe the issue is in order of execution with bind tags. I have juggled order of defined bindtags, but still have one trailing widget handle, after a class level event is focused, then all following class level handles won't call my widget level handle. – Tom Smith Aug 30 '23 at 22:37
  • I have resolved my issue with as described here: https://dafarry.github.io/tkinterbook/tkinter-events-and-bindings.htm. But will update if I find depth of the issue. – Tom Smith Aug 30 '23 at 22:51

0 Answers0