0

I'm having difficulty figuring out how to change the colour of a button in python using tkinter. All the tutorials I've seen show how to change the colour while the button is pressed. For instance

from tkinter import *

root = Tk()
root.title('Testing')
root.geometry("400x400")

btn = Button(root, text = "Change my colour", bg="#00FF00", activebackground="#FF0000")

btn.pack()

root.mainloop()

is my current code and changes the colour from green to red when pressed, but as soon as you lift the mouse it goes back to green. What I'm looking for is how to keep the button red after it has been clicked, and then change it back when it is clicked again. I appreciate any help or advice, thank you!

  • It's unclear what you need help with. The obvious answer is "create a function to change the color". Are you asking how to create a function? Are you asking how to change one of the configuration options? All of these things are documented. – Bryan Oakley Dec 08 '21 at 16:35

1 Answers1

0

Probably the easiest way to implement a dual colored button is to use a Checkbutton. Try this.

tk.Checkbutton(root, indicatoron = 0, activebackground = 'black', background = "green", selectcolor = "red", text  = "Button", foreground = "yellow").pack()

Note the use of Checkbutton indicator = 0 to make it look like a standard button object.

Derek
  • 1,916
  • 2
  • 5
  • 15