How to change the color of a button using tkinter

I keep getting the following error: AttributeError: object "NoneType" does not have the attribute "configure"

# 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 color""" self.button.configure(bg = "red") 
+7
source share
2 answers

When you do self.button = Button(...).grid(...) , what self.button is assigned is the result of the grid() command, not a reference to the created Button object.

You need to assign self.button variable before its packing / grid. It should look something like this:

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

the system is correct. The correct way to format the button is ".config". Not '.configure'

0
source

All Articles