José M.
José M.

Reputation: 623

How to know in what row is a label on a grid in Tkinter?

I have a table (grid) I'm creating, constituted of labels.

These labels are showing elements I'm adding to a list, therefore, when I add a new obj to the list, the table will grow, showing more labels.

My intent is that I can click a label and have it print the row of the table that that label is in.

import tkinter as tk

phrasesdb = []

def debug(event):
    #this is where I'd have it print the row
    #but how do I get it?
    #for example, it the label I pressed was in the row 2, it'd print 2
    print( ??? )

#Add obj to list
def addline():
    ##This creates new obj with values from the input fields, and inserts it in the list
    newobj = {"text": newtext.get()} #This is getting the text from an Entery
    phrasesdb.append(newobj)

    ##This shows new obj on the table
    newesttext = tk.Label(tableframe, text=newobj["text"])
    newesttext.grid(row=len(phrasesdb), column=1, sticky=tk.W)

    newesttext.bind("<Double-Button-1>", debug)

I'm already able to show them in the table, and to have it recognize I'm pressing the correct label (tested with a simple print("yup, this is it") ), but I'm not being able to figure out how to access the row of the label I'm clicking.

I'm kinda new to python and especially tkinter, so sorry if this is a really easy question, but I'm not finding the answer anywhere else...

Upvotes: 1

Views: 1385

Answers (2)

scotty3785
scotty3785

Reputation: 7006

You can use the grid_info method which will return a dictionary of the item's grid attributes.

def debug(event):
    widget = event.widget
    info = widget.grid_info()
    row = info['row']

Upvotes: 2

Chris S
Chris S

Reputation: 42

If I understand your problem correctly using .grid_info()['row'] on the label you already received after clicking should return the result you need.

Upvotes: 0

Related Questions