当所有单选按钮位于同一变量下时,取消选择它们

2024-10-02 18:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

在tkinter中,我创建了带有for循环的单选按钮,这样它们都有相同的变量来调用方法

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循环的所有选项?在


Tags: textinnewforvalueplacewidthvariable
1条回答
网友
1楼 · 发布于 2024-10-02 18:18:35

我找到了一种解决问题的方法,将每个单选按钮存储在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()

相关问题 更多 >