2011-04-04 51 views
7

我不断收到以下错误: AttributeError的:“NoneType”对象有没有属性“配置”如何更改按钮的颜色与Tkinter的

# create color button 
self.button = Button(self, 
        text = "Click Me", 
        command = self.color_change, 
        bg = "blue" 
        ).grid(row = 2, column = 2, sticky = W) 

def color_change(self): 
    """Changes the button's color""" 

    self.button.configure(bg = "red") 

回答

11

当你做self.button = Button(...).grid(...),什么被分配到self.button是结果的grid()命令,而不是Button对象的引用。

您需要在包装/固定它之前指定您的self.button变量。 它应该看起来像这样:

self.button = Button(self,text="Click Me",command=self.color_change,bg="blue") 
self.button.grid(row = 2, column = 2, sticky = W) 
+0

优秀,这是修复!非常感谢! – 2011-04-04 20:41:45

+0

如果您已经制作好按钮,您如何更改背景? – RhetoricalRuvim 2018-02-01 00:17:49

0

该系统是正确的。格式化按钮的正确方法是'.config'。不''配置'

+0

这真的是一个评论,而不是一个答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 – peacetype 2018-02-20 01:34:07

+0

哦,我明白了。抱歉让你烦恼。我是新来的堆栈溢出。 – 2018-02-24 02:19:51