Tkinter is a Python standard library that is used to create GUI (Graphical User Interface) applications. It is one of the most commonly used packages of Python. Tkinter supports both traditional and modern graphics support with the help of Tk themed widgets. All the widgets that tkinter has also available in tkinter.ttk.
Adding style in a tkinter.ttk button is little creepy because it doesn’t support direct implementation. To add styling in a ttk.Button we have to first create a object of style class which is available in tkinter.ttk.
We can create ttk.Button by using the following steps:
btn = ttk.Button(master, option = value, ...)
ttk.Button options –
command: A function to be called when button is pressed.
text: Text which appears on the Button.
image: Image to be appeared on the Button.
style: Style to be used in rendering this button.
To add styling on the ttk.Button we cannot directly pass the value in the options. Firstly, we have to create a Style object which can be created as follows:
style = ttk.Style()
Below code will be adding style to only selected Buttons i.e, only those buttons will get changed in which we will be passing style option.
Only one button will get styled because in the above code we are providing styling only in one Button.
Code #2 Apply style on all the available buttons
Now if you want to change the appearance of the buttons by the movement of the mouse i.e, now when we hover the mouse over the button it will change its color when we press it will change color, and so on.
Code #3 Change color on mouse hover
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course