如何启用/禁用复选框

时间:2014-11-03 12:40:58

标签: python python-2.7 checkbox tkinter state

我对此代码有些困难。

我只想在一些过程之后使一些小部件处于活动状态。现在我可以启用按钮,但不能启用复选框,我无法找出我做错了什么。

我制作了这个简单的代码来说明问题:

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 ['状态'] ='正常' TypeError:' NoneType'对象不支持项目分配

1 个答案:

答案 0 :(得分: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()