当单击任何其他CheckButton时,如何禁用和检查一个CheckButton。
我想在将chk1
设置为“已检查”时也将chk2
设置为“已检查,如果我未选中,我希望将chk1
的状态设置为正常。”>
我在使用以下命令时遇到错误-
import tkinter as tk from tkinter import ttk class Root(tk.Tk): def __init__(self): super().__init__() var1 = tk.IntVar() chk1 = ttk.Checkbutton(self, text = 'Class', variable = var1) #chk1.state(['selected']) chk1.pack(side = 'left') var2 = tk.IntVar() chk2 = ttk.Checkbutton(self, text = 'Section', variable = var2, command = lambda: self.chk(chk1, var2)) #chk2.state(['selected']) chk2.pack(side = 'left') def chk(self, obj, self_val): if self_val.get() == 1: #changed from 0 to 1 obj.state['selected'] obj.configure(state = 'disabled') else: obj.configure(state = 'normal') if __name__ == '__main__': Root().mainloop()
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Sagar\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "d:\chkbtn.py", line 18, in <lambda> command = lambda: self.chk(chk1, var2)) File "d:\chkbtn.py", line 24, in chk obj.state['selected'] TypeError: 'method' object is not subscriptable
输出*经过一些更改后,当单击任何其他CheckButton时,如何使一个CheckButton禁用并检查。我也想在将chk2设置为Checked时将chk1设置为Checked ...
我认为经过一番困惑之后,您需要在lambda和方法中添加第三个变量。