Python 3 Tkinter to Change CSS or Background Color of Button When Pressed or Clicked By User in GUI Desktop App Full Project For Beginners

Python 3 Tkinter to Change CSS or Background Color of Button When Pressed or Clicked By User in GUI Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be changing css or background color of button when pressed or clicked by user in tkinter gui desktop app in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

pip install tkinter

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

import itertools
import tkinter as tk

style_1 = {'fg': 'red', 'bg': 'black', 'activebackground': 'gold', 'activeforeground': 'dim gray'}
style_2 = {'fg': 'yellow', 'bg': 'grey', 'activebackground': 'chocolate', 'activeforeground': 'blue4'}
style_cycle = itertools.cycle([style_1, style_2])


def switch_style():
    style = next(style_cycle)
    button_1.configure(**style)


win = tk.Tk()

button_1 = tk.Button(win, text="style switch", command=switch_style)
button_1.pack(padx=50, pady=50)

win.mainloop()

 

 

If you execute the above python script by typing the below command

See also  Python 3 Script to Find Length of List Using Len() Method Full Example For Beginners

 

python app.py

 

 

Leave a Reply