I've provided the minimum buggy code I could reproduce.
I'm using python 3.
I have a function printValue that outputs the parameter it receives. I also have a list of values.
Using tkinter, I created a frame and am generating a table within the frame based on values. The trouble arises when I have to add a button that outputs the row number. Each button is outputting the value 5 when they should output the corresponding row number.
import tkinter as tk
def printValue(param):
print(param)
Values=[[1],[2],[3],[4],[5]]
mainGuiWindow = tk.Tk()
frame = tk.Frame(mainGuiWindow)
frame.pack()
def refreshFrame():
global frame
global Values
frame.destroy()
frame = tk.Frame(mainGuiWindow)
frame.pack()
tk.Label(frame,text="Sl. No").grid(row=0,column=0)
tk.Label(frame,text="Value").grid(row=0,column=1)
tk.Label(frame,text="Print Entry").grid(row=0,column=2)
numberOfEntries = 0
for entry in Values:
numberOfEntries += 1
tk.Label(frame,text=numberOfEntries).grid(row=numberOfEntries,column=0)
tk.Label(frame,text=entry[0]).grid(row=numberOfEntries,column=1)
tk.Button(frame, text="Print",command=lambda: printValue(numberOfEntries)).grid(row=numberOfEntries,column=2)
refreshFrame()
mainGuiWindow.mainloop()
Problem seems to be that the function parameter in button is binded to the variable 'numberOfEntries'
How do I pass the parameter in the button such that the function prints the correct value? I don't want to modify the function definition. I need to change the way it is called by the button.