I am using a for loop and I would like to pass an individual argument to each separate button (in tkinter), this is for a calculator. However, I am unable to find a solution, to make my problem clear I will show the relevant code:
import tkinter as tk
entry_box = tk.Entry(master,width=35)
def insert_value(value):
entry_box.insert(0,value)
buttons = []
for i in range(0,10):
a = tk.Button(master,text=str(i),command=lambda:insert_value(str(i)))
buttons.append(a)
Note: I have removed the .grid() statements in the loop as it removes quite a large and unnecessary portion of code. And also I want each button to insert its displayed value so button 1 would insert 1, 2: 2 etc.
The problem occurs when passing "i" to insert value, as the value inserted into the entry box is always 9. I think this is because "i" will always end on 9 in the loop.
However, I can not find a solution to this. I have tried getting values from lists. I can't think of any other way to do this( in a loop that is). I know I could just write the statements out 10 times but I am interested to know whether their is a solution.
TL;DR The value passed to insert value is always 9. Is there a way to insert the value that each button displays (1 to 9) into the entry box using a for loop?
Regardless of whether it's right all suggestions would be interesting and nice to see. Thankyou.