3

当我尝试更改第一个旋转框的值时,我使用 for 循环创建了旋转框,它也会反映在另一个旋转框中。我该如何纠正它。提前感谢您的回答。

 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()

[得到这个输出]] click_to_view_output

我想分别更改旋转框get()的值和值。

我需要改变的值应该显示在旋转框中,并且当我们get()旋转框值时应该打印相同的值。

4

2 回答 2

1
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()
于 2021-07-15T09:11:14.993 回答
0

您需要为每个动态更改文本变量 -

spin_num = 0
for prds in products:
     spin_num += 1
     def change():
          print(entry.get())
     entry = Spinbox(frm1, textvariable=f'{spin_num}', from_=1, to=10, command=change)
     entry.pack()

这将更改每个 spin_box 的文本变量的值。这将起作用

于 2021-07-15T08:41:18.510 回答