In this article we want to learn about Tkinter Themes & Styling also we are going to learn about Customizing Look and Feel, so as you know Tkinter is popular GUI framework for Python and it is used to create desktop applications. it provides different types of widgets and tools to create interactive applications. Tkinter also offers the ability to customize the look and feel of the GUI through themes and styling. In this article we are going to learn how to use Tkinter’s themes and styling to create custom GUIs.
Themes
Themes are pre-defined set of colors and fonts that can be applied to your GUI. Tkinter provides several built in themes that you can use including Clam, Alt and Default. you can apply theme to your GUI using the ttk.Style() function.
1 2 3 4 5 6 7 8 9 10 11 |
import tkinter as tk from tkinter import ttk root = tk.Tk() style = ttk.Style() style.theme_use('clam') # Create widgets here root.mainloop() |
In this example, we have applied Clam theme to our GUI using style.theme_use(‘clam’). you can replace clam with any of other built in themes to change the look of your GUI.
Styling
Styling allows you to customize the appearance of individual widgets in your GUI. you can change the colors, fonts and other attributes of widget using the ttk.Style() function.
1 2 3 4 5 6 7 8 9 10 11 12 |
import tkinter as tk from tkinter import ttk root = tk.Tk() style = ttk.Style() style.configure('TButton', foreground='red', font=('Arial', 16)) button = ttk.Button(root, text='GeeksCoders') button.pack() root.mainloop() |
In this example we have styled ttk.Button widget to have red text and font size of 16 using style.configure(). you can replace TButton with the name of any other widget to style it. you can also apply multiple styles to widget by separating them with spaces.
Run the complete code and this will be the result
Custom Themes and Styling
In addition to the builtin themes and styles, you can create custom themes and styles for your GUI. for creating custom theme, you will need to define set of colors and fonts and register them with ttk.Style(). you can then apply your custom theme to your GUI using style.theme_create() and style.theme_use().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import tkinter as tk from tkinter import ttk root = tk.Tk() style = ttk.Style() style.theme_create('my_theme', parent='alt', settings={ 'TButton': {'configure': {'foreground': 'blue'}}, 'TLabel': {'configure': {'background': 'yellow'}} }) style.theme_use('my_theme') button = ttk.Button(root, text='Click me') button.pack() label = ttk.Label(root, text='Geekscoders.com') label.pack() root.mainloop() |
In this example we have created custom theme called my_theme that changes foreground color of the ttk.Button widget to blue and the background color of ttk.Label widget to yellow. after that we have applied this theme to our GUI using style.theme_use(my_theme).
Run the complete code and this will be the result
Learn More on TKinter
- How to Create Conutdown Timer with Python & TKinter
- Create GUI Applications with Python & TKinter
- Python TKinter Layout Management
- How to Create Label in TKinter
- How to Create Buttin in Python TKinter
- Build Music Player in Python TKinter
- Python GUI Programming with TKinter
- TKinter VS PyQt, Which one is Good
- Creating Custom Widgets in TKinter
In result we can say that Tkinter’s themes and styling capabilities provides powerful way to customize the look and feel of your GUI. Whether you are using one of the builtin themes or creating your own custom theme, you can create unique and attractive interface for your desktop application. Experiment with different styles and themes to find the perfect look for your GUI.