Ru44Be4N7
Ru44Be4N7

Reputation: 47

How to know in which entry box on a list did I press enter?

I'm trying to make a table with Entry widgets in Tkinter, so I made a list with them. The problem is that I need to know in which box did the user press Enter to perform certain actions, for example if someone erases the content of the first cell of a row and press Enter, I want to erase the content of all the other cells in that row, or if someone writes the product code in the first cell of that row, the product description should appear on the cell in front.

I've tried with the methodology suggested here: Tkinter binding a function with arguments to a widget by unutbu but it doesn't seem to be working with my list of entry widgets.

Here is what I've tried:

from tkinter import *

root = Tk()
root.configure(background='gray')

item_list_fr = Frame(root, bg='gray', width=450, height=108, padx=3, pady=3)
item_list_fr.grid(row=2, column=0, rowspan=2)

info_tupple=[]

def show(eff=None, row_2=0, column_2=0):
    print(str(row_2), str(column_2))

for row in range(15):
    info_list=[None]*5
    for column in range(5):
        info_list[column]=Entry(item_list_fr)
        info_list[column].bind('<Return>', lambda eff: show(eff, row_2=row, column_2=column))
        info_list[column].grid(row=row+1, column=column)
    info_tupple.append(info_list)

root.mainloop()

How can I rewrite my code to make it work?

Upvotes: 0

Views: 44

Answers (1)

TheLizzard
TheLizzard

Reputation: 7680

You can use functools.partial like this:

from tkinter import *
import functools

root = Tk()
root.configure(background='gray')

item_list_fr = Frame(root, bg='gray', width=450, height=108, padx=3, pady=3)
item_list_fr.grid(row=2, column=0, rowspan=2)

info_tupple=[]

def show(eff=None, row_2=0, column_2=0, event=None):
    print(str(row_2), str(column_2))

for row in range(15):
    info_list=[None]*5
    for column in range(5):
        info_list[column]=Entry(item_list_fr)
        command = functools.partial(show, row_2=row, column_2=column)
        info_list[column].bind('<Return>', command)
        info_list[column].grid(row=row+1, column=column)
    info_tupple.append(info_list)

root.mainloop()

For more answers look here.

Upvotes: 1

Related Questions