I am generating many instances of ttk.Spinbox inside a loop, and am having no trouble making each one "work" independently of the others. I would love to be able to keep a running sum of each row of spinboxes, where some columns are weighted higher than others. Here is a sample code that is trying to accomplish this.
# spinboxes.py
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
j = 0
rows = ['test', 'test2']
columns = ['1','2']
for row in rows:
label = ttk.Label(root, text=row)
label.grid(row=j, column=0)
i=1
running_total = ttk.Entry(root)
running_total.insert(0,0)
for col in columns:
label = ttk.Label(root, text=col)
label.grid(row=j, column=i)
def update(inc):
total = running_total
current = int(total.get())
total.delete(0, tk.END)
total.insert(0, current + inc)
spinbox_1 = ttk.Spinbox(root, from_=0, to=50,command=lambda: update(1)) # multiples of 1
spinbox_2 = ttk.Spinbox(root, from_=0, to=50,command=lambda: update(5)) # multiples of 5
spinbox_1.grid(row=j, column=i+1)
spinbox_2.grid(row=j, column=i+2)
i+=3
running_total.grid(row=j,column=i)
j+=1
root.mainloop()
The problem is it seems to always update the latest running_total Entry object created. I assumed that defining the update function inside the loop would ensure that each pair of spinboxes would have a "unique" command, but cannot figure out how to make this work as intended. Here is a picture that highlights the problem.