How do I code what my mouse click? I managed to do e.get() by using user to type but now, I want to do a mouse version

Viewed 62

I am doing a GUI Tkinter project. Currently. I am able to display the excel file on my GUI via treeview function. I want to code in a way that my mouse click for example, when row 1 is clicked, it will read row1, column 2 box and look for the file in the documents based on the word in row1 x column 2 box and plot some graphs which is completed/ not important, or when row x is left-clicked, lock in row x X column 2 to find the name and search for it in my documents file. I want to code the e.get() but mouse click version.

df = pd.read_csv("C:/Users/njj95/Documents/"+e1.get()+".csv")

What I have tried so far is left click and right click.

def leftClick(event):
    print("left")    
def rightClick(event):
    print("right")
my_tree.bind("<Button-1>",leftClick)
my_tree.bind("<Button-3>",rightClick)

What I have done previously was to user user to enter name and continues from there. Now I want to upgrade my GUI by just clicking the list of usernames instead of user typing in the username. Previously, my code was

df = pd.read_csv("C:/Users/njj95/Documents/"+e1.get()+".csv")

The e1.get() is entered by user.

This is the user type in version GUI

This is an exact copy and paste from an excel file

1 Answers

You can use the event.y to find which row is clicked inside leftClick() function, then use .item() to get the asset ID value from the returned values:

def leftClick(event):
    # find which row is clicked
    row = event.widget.identify_row(event.y)
    if row:
        # get the asset ID (first value in "values")
        asset_id = event.widget.item(row)["values"][0]
        print(asset_id)
    else:
        print("No record clicked")

my_tree.bind("<Button-1>", leftClick)
Related