Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between the widgets of tkinter and tkinter.ttk in Python?
tkinter.ttk is a themed widget module that provides modern-styled alternatives to standard tkinter widgets. While tkinter provides basic GUI components, tkinter.ttk offers enhanced visual styling and cross-platform native appearance.
Key Differences
Here are the major differences between tkinter and tkinter.ttk widgets −
Visual Styling: tkinter widgets have basic appearance, while ttk widgets follow the operating system's native theme for a more modern look.
Widget Variety: ttk provides additional widgets like Combobox, Progressbar, Treeview, and Notebook that aren't available in basic tkinter.
Theming Support: ttk widgets can be easily themed and styled using the Style() class, while tkinter widgets have limited styling options.
Configuration Options: Some configuration options differ between tkinter and ttk widgets (e.g., 'bg' vs 'background').
Import Method: Use
from tkinter.ttk import *to override basic tkinter widgets with their ttk equivalents.
Example: Comparing tkinter and ttk Widgets
Let's create an example showing both tkinter and ttk widgets side by side ?
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.title("tkinter vs ttk Widgets")
root.geometry("400x300")
# Create frames for organization
frame1 = tk.Frame(root)
frame1.pack(pady=10)
frame2 = tk.Frame(root)
frame2.pack(pady=10)
# tkinter widgets
tk.Label(frame1, text="Standard tkinter Widgets", font=('Arial', 12, 'bold')).pack()
tk.Button(frame1, text="tkinter Button", bg="lightblue").pack(pady=5)
tk.Entry(frame1, bg="lightyellow").pack(pady=5)
# ttk widgets
tk.Label(frame2, text="Themed ttk Widgets", font=('Arial', 12, 'bold')).pack()
ttk.Button(frame2, text="ttk Button").pack(pady=5)
ttk.Entry(frame2).pack(pady=5)
# Style configuration for ttk
style = ttk.Style()
style.configure('Custom.TButton', foreground='red', font=('Arial', 10, 'bold'))
# Custom styled ttk button
ttk.Button(frame2, text="Styled ttk Button", style='Custom.TButton').pack(pady=5)
root.mainloop()
Styling ttk Widgets
The ttk.Style class allows you to customize widget appearance ?
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("TTK Styling Example")
root.geometry("300x200")
# Create style object
style = ttk.Style()
# Configure different styles
style.configure('Success.TButton', foreground='green', font=('Arial', 12))
style.configure('Warning.TLabel', foreground='orange', font=('Arial', 10, 'bold'))
# Apply styles to widgets
ttk.Label(root, text="Styled Label", style='Warning.TLabel').pack(pady=10)
ttk.Button(root, text="Success Button", style='Success.TButton').pack(pady=10)
# Default ttk widgets
ttk.Entry(root).pack(pady=10)
ttk.Progressbar(root, length=200, mode='indeterminate').pack(pady=10)
root.mainloop()
Widget Availability Comparison
| Widget Type | tkinter | ttk | Additional Features in ttk |
|---|---|---|---|
| Button | ? | ? | Native OS styling |
| Label | ? | ? | Better font rendering |
| Entry | ? | ? | Placeholder text support |
| Combobox | ? | ? | Dropdown selection |
| Progressbar | ? | ? | Progress indication |
| Treeview | ? | ? | Hierarchical data display |
Best Practices
Use ttk widgets for modern applications that need native OS appearance
Mix tkinter and ttk widgets when needed - they can coexist in the same application
Use ttk.Style() for consistent theming across your application
Import specific widgets to avoid conflicts:
from tkinter import ttk
Conclusion
Use tkinter.ttk for modern, native-looking applications with enhanced styling capabilities. While tkinter provides basic functionality, ttk offers better visual appeal and additional widget types like Combobox and Progressbar.
