How to Change Background Color of Tkinter Button during Mouse Click?

Python Tkinter Button – Change Background Color during Mouse Click

You can change the button’s background color, while the button is pressed using mouse, using activebackground property of the button.

Example 1: Change Button Background Color during Mouse Click

In this example, we will change the color of button to red while it is in pressed state.

Python Program

from tkinter import *   

tkWindow = Tk()  
tkWindow.geometry('400x150')  
tkWindow.title('PythonExamples.org - Tkinter Example')
  
button = Button(tkWindow, text = 'Submit', bg='#ffffff', activebackground='#00ff00')  
button.pack()  
  
tkWindow.mainloop()

#00ff00 mean green color.

Output

Example 2: Change Button Background Color during Mouse Click

In this example, we will change the color of button to red color while it is in pressed state.

Python Program

from tkinter import *   

tkWindow = Tk()  
tkWindow.geometry('400x150')  
tkWindow.title('PythonExamples.org - Tkinter Example')
  
button = Button(tkWindow, text = 'Submit', bg='#ffffff', activebackground='#4444ff')  
button.pack()  
  
tkWindow.mainloop()

Output

Example 3: Change Button Background Color during Mouse Click

In this example, we will change the color of button to red color while it is in pressed state.

Python Program

from tkinter import *   

tkWindow = Tk()  
tkWindow.geometry('400x150')  
tkWindow.title('PythonExamples.org - Tkinter Example')
  
button = Button(tkWindow, text = 'Submit', bg='#ffffff', activebackground='red')  
button.pack()  
  
tkWindow.mainloop()

For the standard colors, you can provided the name of the color. Like red, black, green, yellow, etc.

Summary

In this tutorial of Python Examples, we learned to change background color of a button during its state of pressed.