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

PythonTkinterServer Side ProgrammingProgramming

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.

Example

#Import the tkinter library
from tkinter import *
from tkinter import ttk
#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()

Output

Running the above code 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.

raja
Published on 22-Apr-2021 07:26:53
Advertisements