当它们位于同一变量

时间:2016-12-11 16:41:24

标签: python tkinter

在tkinter中我创建了带有for循环的radiobuttons,所以它们都有相同的变量来调用

上的方法
for z in posOne: #chief
    r = Radiobutton(new, text=z, variable=chief, value=z, indicatoron=0, width=15) 
    r.place(x=60, y=chiefY)
    chiefY += 25

for z in posTwo: #assistant chief
    r = Radiobutton(new, text=z, variable=asChief, value=z, indicatoron=0, width=15) 
    r.place(x=240, y=asChiefY)
    asChiefY += 25

for z in posThree: #captain
    r = Radiobutton(new, text=z, variable=cap, value=z, indicatoron=0, width=15) 
    r.place(x=420, y=capY)
    capY += 25

for z in posFour: #first lieutenant
    r = Radiobutton(new, text=z, variable=lieut, value=z, indicatoron=0, width=15) 
    r.place(x=600, y=lieutY)
    lieutY += 25

当我拨打r.deselect()时,它只取消选择最后一个无线电按钮。如何通过我已经设置的for循环取消选择所有这些?

1 个答案:

答案 0 :(得分:0)

我找到了一种方法来解决我的问题,方法是将每个单选按钮存储在for循环中的列表中,然后调用for循环来取消选择该列表中的所有内容

     arr = []
     for z in posOne: #chief
        r = Radiobutton(new, text=z, variable=chief, value=z, indicatoron=0, width=15) 
        r.place(x=60, y=chiefY)
        chiefY += 25
        arr.append(r)

    for z in posTwo: #assistant chief
        r = Radiobutton(new, text=z, variable=asChief, value=z, indicatoron=0, width=15) 
        r.place(x=240, y=asChiefY)
        asChiefY += 25
        arr.append(r)

    for z in posThree: #captain
        r = Radiobutton(new, text=z, variable=cap, value=z, indicatoron=0, width=15) 
        r.place(x=420, y=capY)
        capY += 25
        arr.append(r)

    for z in posFour: #first lieutenant
        r = Radiobutton(new, text=z, variable=lieut, value=z, indicatoron=0, width=15) 
        r.place(x=600, y=lieutY)
        lieutY += 25
        arr.append(r)

    for x in arr:
        x.deselect()