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?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With