Search code examples
tkintertableviewttkbootstrap

Unable to .bind("<<TreeviewSelect>>".. to work with TableView to get selected row data


I am trying to get the row data of the selected row in a ttkbootstrap Tableview, and as with tkinter treeview, I've always just used .bind("<>",my_func) and my_func would proceed in doing the work.

Here is the code I used, and there is no indication the on_tv_select ran when the selecting a row from the Tableview. In that case, I'm not clear on how to obtain the selected row data.

import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview

def on_tv_select(event):
    print(f".. I was selected")

if __name__ == '__main__':
    colData = [{'text': 'carID', 'stretch': True},
               {'text': 'Make', 'stretch': True},
               {'text': 'Model', 'stretch': True}]
    rowData = [(1,'Make ZZ', 'Model BA'),
               (2,'Make DD', 'Model Ed'),
               (3,'Z Dude','That other')]
    mainWindow = tk.Tk()
    mainWindow.title("POC-Main Window")

    myTableView = Tableview(master=mainWindow,coldata=colData,rowdata=rowData,paginated=True,searchable=True,bootstyle='primary')
    myTableView.pack(fill='both', expand=True, padx=10, pady=10)
    myTableView.bind("<<TreeviewSelect>>",on_tv_select)
    mainWindow.mainloop()

I did come about this post How do I get contents of selected row using Python and ttkbootstrap's tableview? which showed bind_all though binding to all widgets introduces further issues with my application.


Solution

  • According to the source code of Tableview, it is actually a ttk.Frame with a ttk.Treeview inside and this internal Treeview widget can be referenced by attribute view.

    So bind the event to this internal treeview instead:

    myTableView.view.bind("<<TreeviewSelect>>", on_tv_select)