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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
python app.py