RRSC
RRSC

Reputation: 257

Getting Index Number from pandastable

I am trying to display pandas dataframe in tkinter window and I got to know about pandastable Library. As of now I am able to display my dataframe in the pandastable using the below code but I am stuck in an issue. Now if I select/click on some index in the pandastable the corresponding row gets highlighted in the table. Issue is I wanted to get the selected Index number from the pandastable when I click on a particular index in the pandastable.

As of now I tried get_row_clicked(event) & getSelectedRow() but its giving me only row number clicked/selected.

import tkinter as tk
from tkinter import *
from pandastable import Table, TableModel
import pandas as pd

df = pd.read_csv(path to CSV file)

app = Tk()
app.geometry('600x400+200+100')
app.title('Table app')
f = tk.Frame(app)
f.pack(fill=BOTH,expand=1)

table = pt = Table(f, dataframe=df,
                        showtoolbar=True, showstatusbar=True)

pt.show()

def handle_left_click(event):   
    rowclicked = pt.get_row_clicked(event)
    print("RowClicked", rowclicked)
    rowsel = pt.getSelectedRow()
    print("RowSelected", rowsel) 

table.bind("<Button-1>",handle_left_click)

app.mainloop()

I expect to get index value as 2 if I click on the second index of the pandastable and so on.

Any help would be highly appreciated.

Upvotes: 2

Views: 2014

Answers (1)

RRSC
RRSC

Reputation: 257

Got the solution... There is something called table.rowheader.bind event in the documentation which gives us the required index number when it is selected. I have used the following code to get the solution:

from pandastable import Table, TableModel
import tkinter as tk
import pandas as pd

df = pd.read_file("Read your CSV file")

app = tk.Toplevel()

app.title('Table app')
f = tk.Frame(app)
f.pack(fill=BOTH,expand=1)

table = pt = Table(f, dataframe=df, showtoolbar=True, showstatusbar=True)

pt.show()

def handle_left_click(event):
    """Handle left click"""
    rowclicked_single = table.get_row_clicked(event)
    print(rowclicked_single)
    table.setSelectedRow(rowclicked_single)
    table.redraw()

pt.rowheader.bind('<Button-1>',handle_left_click)

app.mainloop()

Upvotes: 3

Related Questions