2011-04-04 37 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