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.

Examples

1. Change button background to green 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()
Copy

#00ff00 mean green color.

Output

2. Change button background color to specified HEX 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()
Copy

Output

3. Change button background color to ‘red’ 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()
Copy

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.

Related Tutorials

Code copied to clipboard successfully 👍