pythontkinterspinbox

spin box values reflected in another spin box made by for loop


i have created spin boxes using for loop when i tried to change the value of first spin box it will also reflect in another spin box. how can i rectify it. thanks in advance for answers.

 from tkinter import *
 win =Tk()
 frm1 = Frame(win,bg='sky blue')
 frm1.pack(fill='both',expand=1)
 products = [1,2,3,4,5]
 for prds in products:
     def change():
         print(entry11_cart.get())
     entry11_cart = Spinbox(frm1, textvariable=1, from_=1, to=10, command=change)
     entry11_cart.pack()
 win.mainloop()

[got this output]]click_to_view_output

i want to change the values of spin boxes separately and get() the values separately.

i need the changing value should be shown in spin box and the same value should print when we get() the spin box value.


Solution

  • from tkinter import *
    w=Tk()
    frm1 = Frame(w,bg='sky blue')
    frm1.pack(fill='both',expand=1)
    #the names of spinbox
    l=["name_1","name_2"]
    e=["apple","pine"]
    #don't use for i in l
    #it might assign all spinboxes' name to "i"
    for i in range(len(l)):
        def change(i):
            print(l[i].get())
        e[i]=StringVar()
        l[i]=Spinbox(frm1, textvariable=e[i], from_=1, to=10, command=lambda i=i: change(i))
        l[i].pack()
        print(l[i])
    w.mainloop()