如何启用/禁用复选框
我在这段代码上遇到了一些困难。
我只是想在某个过程完成后让一些小部件变得可用。目前我能让按钮变得可用,但复选框却不行,我不知道哪里出错了。
我写了这段简单的代码来说明问题:
import Tkinter as tk
root=tk.Tk()
class Application (tk.Frame):
def __init__(self,master):
tk.Frame.__init__(self, master)
self.grid()
self.create_windgets()
def DoNothing(self):
print ""
def Compute(self):
self.sampleButton['state']='normal'
self.CB1['state']='normal'
def create_windgets (self):
self.sampleButton=tk.Button(self, text="Sample",state='disable' , command=self.DoNothing)
self.sampleButton.grid()
self.EDButton=tk.Button(self, text="Enable", command=self.Compute)
self.EDButton.grid()
self.o1=tk.BooleanVar()
self.CB1=tk.Checkbutton(self,text="submit",state='disable' , variable =self.o1).grid()
app=Application(root)
root.mainloop()
它返回了这个错误信息:
self.CB1['state']='normal' 类型错误:'NoneType'对象不支持项赋值
1 个回答
0
grid
方法确实会返回 None
;你需要把下面这一行分开:
self.CB1=tk.Checkbutton(self,text="submit",state='disable' , variable=self.o1).grid()
变成两条语句:
self.CB1 = tk.Checkbutton(self,text="submit",state='disable' , variable=self.o1)
self.CB1.grid()