【发布时间】:2020-03-16 01:14:15
【问题描述】:
我要构建a user interface like this:
代码是:
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实例。使用Radiobutton而不是Checkbutton更好吗? -
我试过了,还是不行
-
v = [None] * len(solutions) for ii in range(len(solutions)): 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_yes)跨度>
-
无法粘贴完整的代码,因为太长了
-
如何获取
close_yes()和close_no()函数中变量的值?
标签: python tkinter tkinter.checkbutton