Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is changing button color on a press in tkinter possible?

I’m using Python’s tkinter module and I want to change the color of a button only when it's getting pressed. So for an example normally the button would be white, but when I press it, it would turn green. If I stop pressing it, it would turn back to white. Is that possible?

like image 873
Juho Pesonen Avatar asked Aug 31 '25 17:08

Juho Pesonen


1 Answers

The simplest method is to use the "activebackground" property of the button

import tkinter as tk

root = tk.Tk()
btn = tk.Button(root,text="click me",activebackground="red")
btn.grid()
root.mainloop()

This example will set the background color of the button to red when it is being pressed and return to it's original color when released.

In a similar way you get change the default background color.

btn = tk.Button(root,text="click me", background="white", activebackground="red")
like image 114
scotty3785 Avatar answered Sep 05 '25 12:09

scotty3785