Search code examples
pythonooptkintertreeviewttk

Bind functions issue in Tkinter


I have reported a simplified runnable version of my code where I'm trying to bind to my Treeview to perform action on it. My issues are the following: the onSingleClick function is not always triggered, the onDoubleClick function is never triggered.

After a bit of troubleshooting I think the self.tableId = 0 is part of the problem since by removing the initialisation on the constructor the error seem to be less present.

Secondly if the error is fixed the double click on the screen triggers also the single click witch is not optimal for what I'm trying to do and I was wondering if anybody has a clever solution to this.

Thanks for the time and please help me fix this error!

import tkinter as tk
from tkinter import ttk

class TreeviewM(ttk.Treeview):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        
        self.tableId = 0

        # Bind events
        self.bind("<Button-1>", self.on_single_click)
        self.bind("<Double-1>", self.on_double_click)

    def on_single_click(self, event):
        print("Single click event triggered")

    def on_double_click(self, event):
        print("Double click event triggered")

def main():
    root = tk.Tk()
    root.geometry("1000x800")

    columns = ("Name", "Surname")
    table = TreeviewM(root, columns=columns, show="headings")

    # Define columns and headings
    table.column("Name", width=100, anchor="center")
    table.column("Surname", width=100, anchor="center")
    table.heading("Name", text="Name")
    table.heading("Surname", text="Surname")
    
    # Insert data
    table.insert("", "end", values=("John", "Doe"))
    table.insert("", "end", values=("Jane", "Doe"))
    
    table.pack(fill="both", expand=True)

    # Force focus on the widget
    table.focus_set()
    
    root.mainloop()

if __name__ == "__main__":
    main()

Solution

  • I forgot to check for updates for all the libraries witch was actually the problem.