I am writing a simple tkinter widget
which a column of entry boxes and a column of buttons. The button should print the value in the corresponding entry box. I have essentially written all the code, but I have hardcoded the row label into my function:
print find_in_grid(root, 2, 0).get()
I need to instead replace the 2
with the row of the button that was clicked. How can I get that row?
Entire code:
from Tkinter import *
def print_value():
print find_in_grid(root, 2, 0).get()
def find_in_grid(frame, row, column):
for children in frame.children.values():
info = children.grid_info()
#note that rows and column numbers are stored as string
if info['row'] == str(row) and info['column'] == str(column):
return children
return None
root = Tk()
height = 5
width = 1
for i in range(height): #Rows
for j in range(width): #Columns
b = Entry(root, text="", width=100)
b.grid(row=i, column=j)
height = 5
width = 1
for i in range(height): #Rows
for j in range(width): #Columns
b = Button(root, text="print value", command=print_value, width=10)
b.grid(row=i, column=j+1)
mainloop()