有两个复选框。当一个被勾选时,我需要另一个去掉它的勾号,假设它被勾选了。我该怎么做?
我尝试延迟禁用和启用复选框,但它们会恢复到初始状态(如果勾选,则保持勾选状态(。
from tkinter import *
def removetickwoman():
# something to remove the tick from woman
def removetickman():
# something to remove the tick from man
root = Tk()
chkvar1 = IntVar()
chkvar2 = IntVar()
check1 = Checkbutton(root, text="man", variable=chkvar1, command=removetickwoman)
check1.pack()
check2 = Checkbutton(root, text="woman", variable=chkvar2, command=removetickman)
check2.pack()
root.mainloop()
是的,这不仅是可能的,而且已经由tkinter
提出:您需要使用RadioButton
而不是CheckButton
:
import tkinter as tk
root = tk.Tk()
MODES = [("Man", "M"), ("Woman", "W")]
v = tk.StringVar()
v.set("M")
for text, mode in MODES:
b = tk.Radiobutton(root, text=text,
variable=v, value=mode)
b.pack(anchor=tk.W)
root.mainloop()