lengyue869 发表于 2021-6-16 11:09:17

radiobutton 怎么取消默认选择

本帖最后由 lengyue869 于 2021-6-16 22:19 编辑

radiobutton默认会选择第一个,checkbutton有deselect()方法可以取消选择,radiobutton有类似的方法吗?


import tkinter as tk

root=tk.Tk()
root.geometry('400x200+600+100')
plant_list=['a','b','c']

rbText=tk.IntVar()

def plant_pick():
    pass

for plant in plant_list:
    tk.Radiobutton(root, text=plant, value=plant_list.index(plant), variable=rbText,
                   command=plant_pick).grid(row=0, column=plant_list.index(plant))

root.mainloop()

lengyue869 发表于 2021-6-16 13:33:00

tk.Radiobutton(root, text=plant, value=plant_list.index(plant), variable=rbText,
AttributeError: 'NoneType' object has no attribute 'deselect'

lengyue869 发表于 2021-6-16 22:18:40

Ctrl+.


import tkinter as tk

root=tk.Tk()
root.geometry('400x200+600+100')
plant_list=['a','b','c']

rbText=tk.IntVar()
rbText.set(None)
def plant_pick():
    lb.config(text='you have selected '+plant_list)

for plant in plant_list:
    tk.Radiobutton(root, text=plant, value=plant_list.index(plant), variable=rbText,
                   command=plant_pick).grid(row=0, column=plant_list.index(plant))

lb=tk.Label(root,text='',bg='yellow')
lb.grid(row=3,column=0)
root.mainloop()
页: [1]
查看完整版本: radiobutton 怎么取消默认选择