rediobutton清除选项
时间: 2024-01-15 20:04:29 浏览: 8
要清除radiobutton的选项,可以使用`radiobutton.deselect()`方法。例如:
```
import tkinter as tk
root = tk.Tk()
var = tk.StringVar(value="Option 1")
rb1 = tk.Radiobutton(root, text="Option 1", variable=var, value="Option 1")
rb2 = tk.Radiobutton(root, text="Option 2", variable=var, value="Option 2")
rb3 = tk.Radiobutton(root, text="Option 3", variable=var, value="Option 3")
rb1.pack()
rb2.pack()
rb3.pack()
def clear_selection():
var.set(None)
rb1.deselect()
rb2.deselect()
rb3.deselect()
clear_button = tk.Button(root, text="Clear selection", command=clear_selection)
clear_button.pack()
root.mainloop()
```
这个例子中,我们创建了三个radiobutton选项,并且将它们绑定到同一个variable上。当我们点击“Clear selection”按钮时,会清除variable的值(设置为None),并且使用`deselect()`方法清除所有radiobutton的选中状态。