Add style to Python tkinter button

Tkinter has great support for creating GUI programs based on Python. It offers different ways of styling buttons on the Tkinter canvas based on font, size, color, and other properties. In this article, we will see how to apply styles to specific buttons or all buttons in general on the canvas using the ttk.Style class.

Applying Style to Specific Buttons

Let's consider the case when we have two buttons in the canvas and we want to apply styling only to the first button. We use a custom style name like 'W.TButton' as part of the configuration along with font and foreground color properties ?

Example

from tkinter import *
from tkinter.ttk import *

# Set the canvas
root = Tk()
root.geometry('200x150')
root.title('Button Style Example')

# Create style object
style = Style()

# Configure custom style
style.configure('W.TButton', 
               font=('Arial', 10, 'underline'),
               foreground='Green')

# Button with custom style
styled_btn = Button(root, text='Welcome !',
                   style='W.TButton',
                   command=root.destroy)
styled_btn.grid(row=0, column=1, padx=50, pady=10)

# Button without custom style (uses default)
normal_btn = Button(root, text='Click to Start !', command=None)
normal_btn.grid(row=1, column=1, pady=10, padx=50)

root.mainloop()

In this example, only the first button uses the custom 'W.TButton' style with green underlined text, while the second button retains the default appearance.

Applying Style to All Buttons

To apply styling to all buttons on the canvas, we configure the default 'TButton' style. This automatically applies to all ttk.Button widgets that don't specify a custom style ?

Example

from tkinter import *
from tkinter.ttk import *

root = Tk()
root.geometry('200x150')
root.title('Global Button Style')

# Create style object
style = Style()

# Configure default TButton style (applies to all buttons)
style.configure('TButton', 
               font=('Calibri', 10, 'bold', 'underline'),
               foreground='Green')

# Button 1 (uses default TButton style)
btn1 = Button(root, text='Welcome !',
             command=root.destroy)
btn1.grid(row=0, column=1, padx=50, pady=10)

# Button 2 (also uses default TButton style)
btn2 = Button(root, text='Click to start !', command=None)
btn2.grid(row=1, column=1, pady=10, padx=50)

root.mainloop()

In this example, both buttons automatically inherit the global 'TButton' style with bold, underlined green text.

Common Style Properties

Here are some commonly used style properties for buttons ?

from tkinter import *
from tkinter.ttk import *

root = Tk()
root.geometry('300x200')

style = Style()

# Configure a comprehensive button style
style.configure('Custom.TButton',
               font=('Arial', 12, 'bold'),
               foreground='white',
               background='blue',
               padding=10,
               relief='raised',
               borderwidth=2)

custom_btn = Button(root, text='Styled Button',
                   style='Custom.TButton')
custom_btn.pack(pady=20)

root.mainloop()

Comparison

Style Type Style Name Applies To
Custom Style 'CustomName.TButton' Only buttons with this specific style
Global Style 'TButton' All buttons without custom styles

Conclusion

Use custom style names like 'Custom.TButton' to style specific buttons, or configure 'TButton' to apply styling globally to all buttons. The ttk.Style class provides powerful customization options for creating visually appealing GUI applications.

Updated on: 2026-03-15T18:21:01+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements