Python Tkinter Checkbutton问题

Python Tkinter Checkbutton问题,python,tkinter,tkinter.checkbutton,Python,Tkinter,Tkinter.checkbutton,我想建造 代码是: for ii in range(len(solutions)): tk.Label(text=solutions[ii], bg="lightsalmon", fg="black", font=("times", 10), relief=tk.RIDGE, width=50, anchor="w").grid(row=ii+1,column=3, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W) v = Stri

我想建造

代码是:

for ii in range(len(solutions)):
tk.Label(text=solutions[ii], bg="lightsalmon", fg="black", font=("times", 10), relief=tk.RIDGE, width=50, anchor="w").grid(row=ii+1,column=3, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)
v = StringVar()
checkbutton1 = Checkbutton(mywindow, text='YES', onvalue='YES', variable=v, bg="red", fg="black", font=("times", 10), width=3, anchor="w", command=close_yes)
checkbutton1.deselect()
checkbutton1.grid(row=ii+1, column=4, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)
checkbutton2 = Checkbutton(mywindow, text='NO', onvalue='NO', variable=v, bg="red", fg="black", font=("times", 10), width=3, anchor="w", command=close_no)
checkbutton2.deselect()
checkbutton2.grid(row=ii+1, column=5, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)

问题是我只能得到最后一个checkbutton的值,你能帮我解决这个问题吗?多谢各位

您需要使用一个列表来保存
StringVar
实例。要访问
StringVar
内部
close_yes()
close_no()
函数的正确实例,需要使用lambda和lambda参数的默认值向它们传递正确的索引:

def close_yes(i):
    print('close_yes:', i, v[i].get())

def close_no(i):
    print('close_no:', i, v[i].get())

...

v = [None] * len(solutions)  # list to hold the StringVar instances
for ii in range(len(solutions)):
    tk.Label(text=solutions[ii], bg="lightsalmon", fg="black", font=("times", 10), relief=tk.RIDGE,
             width=50, anchor="w").grid(row=ii+1,column=3, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)
    v[ii] = tk.StringVar()
    checkbutton1 = tk.Checkbutton(mywindow, text='YES', onvalue='YES', variable=v[ii],
                                  bg="red", fg="black", font=("times", 10), width=3, anchor="w",
                                  command=lambda i=ii: close_yes(i)) # use lambda to pass the correct index to callback
    checkbutton1.deselect()
    checkbutton1.grid(row=ii+1, column=4, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)
    checkbutton2 = tk.Checkbutton(mywindow, text='NO', onvalue='NO', variable=v[ii],
                                  bg="red", fg="black", font=("times", 10), width=3, anchor="w", 
                                  command=lambda i=ii: close_no(i))
    checkbutton2.deselect()
    checkbutton2.grid(row=ii+1, column=5, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)

您需要使用一个列表来保存
StringVar
实例。要访问
StringVar
内部
close_yes()
close_no()
函数的正确实例,需要使用lambda和lambda参数的默认值向它们传递正确的索引:

def close_yes(i):
    print('close_yes:', i, v[i].get())

def close_no(i):
    print('close_no:', i, v[i].get())

...

v = [None] * len(solutions)  # list to hold the StringVar instances
for ii in range(len(solutions)):
    tk.Label(text=solutions[ii], bg="lightsalmon", fg="black", font=("times", 10), relief=tk.RIDGE,
             width=50, anchor="w").grid(row=ii+1,column=3, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)
    v[ii] = tk.StringVar()
    checkbutton1 = tk.Checkbutton(mywindow, text='YES', onvalue='YES', variable=v[ii],
                                  bg="red", fg="black", font=("times", 10), width=3, anchor="w",
                                  command=lambda i=ii: close_yes(i)) # use lambda to pass the correct index to callback
    checkbutton1.deselect()
    checkbutton1.grid(row=ii+1, column=4, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)
    checkbutton2 = tk.Checkbutton(mywindow, text='NO', onvalue='NO', variable=v[ii],
                                  bg="red", fg="black", font=("times", 10), width=3, anchor="w", 
                                  command=lambda i=ii: close_no(i))
    checkbutton2.deselect()
    checkbutton2.grid(row=ii+1, column=5, ipadx=0, ipady=0, rowspan=1, sticky=tk.N+tk.E+tk.S+tk.W)

您可以使用列表来保存
StringVar
实例。使用
Radiobutton
而不是
Checkbutton
更好吗?我尝试过但没有成功v=[None]*len(解决方案)用于范围内的ii(len(解决方案)):v[ii]=StringVar()checkbutton1=Checkbutton(mywindow,text='YES',onvalue='YES',variable=v[ii],bg=“red”,fg=“black”,font=(“times”,10),width=3,anchor=“w”,command=close\u yes)无法粘贴完整的代码,因为太长了如何获取
close\u yes()
close\u no()
函数中的变量值?可以使用列表保存
StringVar
实例。使用
Radiobutton
而不是
Checkbutton
更好吗?我尝试过但没有成功v=[None]*len(解决方案)用于范围内的ii(len(解决方案)):v[ii]=StringVar()checkbutton1=Checkbutton(mywindow,text='YES',onvalue='YES',variable=v[ii],bg=“red”,fg=“black”,font=(“times”,10),width=3,anchor=“w”,command=close\u yes)无法粘贴完整的代码,因为太长了如何获取函数中变量的值!非常感谢你!我在这里呆了好几天!天哪,解决了!非常感谢你!我在这里呆了好几天!