The Wayback Machine - https://web.archive.org/web/20240712165028/https://www.geeksforgeeks.org/python-add-style-to-tkinter-button/
Open In App

Python | Add style to tkinter button

Last Updated : 31 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 also has available in tkinter.ttk.
Adding style in a tkinter.ttk button is a little creepy because it doesn’t support direct implementation. To add styling in a ttk.Button we have to first create an 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.
Code #1: 

Python3




# Import Required Module
from tkinter import *
from tkinter.ttk import *
 
# Create Object
root = Tk()
 
# Set geometry (widthxheight)
root.geometry('100x100')
 
# This will create style object
style = Style()
 
# This will be adding style, and
# naming that style variable as
# W.Tbutton (TButton is used for ttk.Button).
style.configure('W.TButton', font =
               ('calibri', 10, 'bold', 'underline'),
                foreground = 'red')
 
# Style will be reflected only on
# this button because we are providing
# style only on this Button.
''' Button 1'''
btn1 = Button(root, text = 'Quit !',
                style = 'W.TButton',
             command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
 
''' Button 2'''
 
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
 
# Execute Tkinter
root.mainloop()


Output: 
 

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 

Python3




# Import Required Module
from tkinter import *
from tkinter.ttk import *
 
# Create Root Object
root = Tk()
 
# Set Geometry(widthxheight)
root.geometry('100x100')
 
# Create style Object
style = Style()
 
 
# Will add style to every available button
# even though we are not passing style
# to every button widget.
style.configure('TButton', font =
               ('calibri', 10, 'bold', 'underline'),
                foreground = 'red')
# button 1
btn1 = Button(root, text = 'Quit !',
                  style = 'TButton',
             command = root.destroy)
 
btn1.grid(row = 0, column = 3, padx = 100)
 
# button 2
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
 
# Execute Tkinter
root.mainloop()


Output: 
 

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 

Python3




# Import Required Module
from tkinter import *
from tkinter.ttk import *
 
# Create Root Object
root = Tk()
# Set Geometry(widthxheight)
root.geometry('500x500')
 
# Create style Object
style = Style()
 
style.configure('TButton', font =
               ('calibri', 20, 'bold'),
                    borderwidth = '4')
 
# Changes will be reflected
# by the movement of mouse.
style.map('TButton', foreground = [('active', '!disabled', 'green')],
                     background = [('active', 'black')])
 
# button 1
btn1 = Button(root, text = 'Quit !', command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
 
# button 2
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
 
# Execute Tkinter
root.mainloop()


Output: 
 

 



Previous Article
Next Article

Similar Reads

Python | Add image on a Tkinter button
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive. Image can be added with the help of PhotoImage() method. This is a Tkinter method
3 min read
How to add a border color to a button in Tkinter?
In this article, we will learn how to add border color to a button in Tkinter. In the first example, we are using the frame widget to add border color to a button in frame by highlighting the border with black color and a thickness of 2. Example 1: Using Frame widget to add border color to a button. Steps: A frame is a container for widgets. In thi
2 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Tkinter | Adding style to the input text using ttk.Entry widget
Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user. It can be created as follows- entry = ttk.Entry(master, option = value, ...) Code #1: Creating Entry widget and taking input from user (taking only String d
2 min read
Change color of button in Python - Tkinter
Prerequisite: Creating a button in tkinter, Python GUI – tkinter In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods: Using bg properties.Using activebackground properties. Example 1: using bg properties. We can change the button background color with bg properties, Th
1 min read
Open a new Window with a button in Python-Tkinter
Python provides a variety of GUI (Graphic User Interface) such as PyQt, Tkinter, Kivy and soon. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to learn and implement as well. The word Tkinter comes from the tk interface. The tkinter module is available in Python standard library.Note: For more informa
3 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
Python Tkinter - Create Button Widget
The Tkinter Button widget is a graphical control element used in Python's Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked. Note: For more reference, you can read our article: What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter But
4 min read
Comparing Old-Style and New-Style Classes in Python
In Python, the difference between old-style and new-style classes is based on the inheritance from the built-in object class. This distinction was introduced in Python 2.x and was fully adopted in Python 3.x, where all classes are new-style classes. In this article, we will see the difference between the old-style and new-style classes in Python. P
4 min read
How to check which Button was clicked in Tkinter ?
Are you using various buttons in your app, and you are being confused about which button is being pressed? Don't know how to get rid of this solution!! Don't worry, just go through the article. In this article, we will explain in detail the procedure to know which button was pressed. Steps by Step Approach: Step 1: First, import the library Tkinter
3 min read
Practice Tags :
three90RightbarBannerImg