How to reset the background color of a Python Tkinter button?

Tkinter buttons are useful for handling events within the application. We can configure the button properties such as text style, font-family, background color, text color, and text size using predefined properties.

We can reset the background color and other properties by defining a callback function that uses the configure() method to modify the button's appearance.

Basic Example

Here's how to reset a button's background color when clicked ?

# Import the tkinter library
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Define the geometry of the function
win.geometry("750x250")

# Define a function to change the properties of button
def change_color():
    btn.configure(bg="OrangeRed3", fg="white")

# Create a Label
Label(win, text="Click the Button to reset the Color of the Button", font=('Georgia', 16, 'italic')).pack(pady=30)

# Create a button to close the window
btn = Button(win, text="RESET", command=change_color, font=('Georgia', 11))
btn.pack(side=TOP, ipady=5, ipadx=20)

win.mainloop()

Advanced Example with Multiple Colors

You can create buttons that cycle through different colors ?

from tkinter import *

win = Tk()
win.geometry("400x300")
win.title("Button Color Reset Demo")

# List of colors to cycle through
colors = ["lightblue", "lightgreen", "yellow", "pink", "orange"]
current_color = 0

def reset_color():
    global current_color
    btn.configure(bg=colors[current_color])
    current_color = (current_color + 1) % len(colors)

def reset_to_default():
    btn.configure(bg="SystemButtonFace", fg="black")

# Create buttons
btn = Button(win, text="Change Color", command=reset_color, font=('Arial', 12))
btn.pack(pady=20, ipady=10, ipadx=20)

reset_btn = Button(win, text="Reset to Default", command=reset_to_default, font=('Arial', 10))
reset_btn.pack(pady=10)

Label(win, text="Click 'Change Color' to cycle colors\nClick 'Reset to Default' to restore original", 
      font=('Arial', 10)).pack(pady=20)

win.mainloop()

Key Methods

The main methods for resetting button colors are:

  • configure(bg="color") − Changes background color
  • configure(fg="color") − Changes text color
  • config() − Alternative to configure()

Output

Running the first example will display a window that contains a button and a text in it.

Now, click the "RESET" button to change the background as well as the foreground color of the button.

Conclusion

Use the configure() method to reset button colors dynamically. You can change both background (bg) and foreground (fg) colors by calling this method within callback functions.

Updated on: 2026-03-25T19:33:55+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements