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
How to change the font on ttk.Entry in Tkinter?
There are times when a user wants to insert information like Name, contact number, Email, address, etc. Tkinter has a simple way to handle these types of inputs through its Entry widgets. Tkinter Entry widgets can be styled using the ttk package.
To change other properties of the Entry widgets such as font properties, text-size, and font-style, we can use the font('font-family font-size font-style') attribute. We can specify the font property in the entry constructor.
Basic Example
Here's how to create a ttk.Entry with custom font properties ?
# Import tkinter library
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("750x250")
win.title("TTK Entry Font Example")
# Create an Entry Widget with custom font
entry = ttk.Entry(win, font=('Century', 12), width=40)
entry.pack(pady=30)
win.mainloop()
Different Font Styles
You can customize various font properties including family, size, and style ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("800x400")
win.title("Multiple Entry Font Styles")
# Different font examples
fonts = [
('Arial', 14, 'normal'),
('Times New Roman', 16, 'bold'),
('Courier New', 12, 'italic'),
('Helvetica', 18, 'bold italic')
]
labels = ["Normal Arial 14:", "Bold Times 16:", "Italic Courier 12:", "Bold Italic Helvetica 18:"]
for i, (font_family, size, style) in enumerate(fonts):
# Label for each entry
label = ttk.Label(win, text=labels[i])
label.pack(pady=(10, 5))
# Entry with different font
entry = ttk.Entry(win, font=(font_family, size, style), width=50)
entry.pack(pady=(0, 10))
entry.insert(0, f"Sample text in {font_family} {size}pt {style}")
win.mainloop()
Font Syntax Options
The font parameter accepts different formats for flexibility ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("700x300")
win.title("Font Syntax Examples")
# Method 1: Tuple format (family, size, style)
entry1 = ttk.Entry(win, font=('Arial', 14, 'bold'))
entry1.pack(pady=10)
entry1.insert(0, "Tuple format: ('Arial', 14, 'bold')")
# Method 2: String format
entry2 = ttk.Entry(win, font='Times 16 italic')
entry2.pack(pady=10)
entry2.insert(0, "String format: 'Times 16 italic'")
# Method 3: Dictionary format
entry3 = ttk.Entry(win, font={'family': 'Courier New', 'size': 12, 'weight': 'bold'})
entry3.pack(pady=10)
entry3.insert(0, "Dictionary format with weight")
win.mainloop()
Font Properties
| Property | Options | Example |
|---|---|---|
| Family | Arial, Times, Courier, Helvetica | 'Arial' |
| Size | Integer (points) | 12, 14, 16 |
| Weight | normal, bold | 'bold' |
| Slant | roman, italic | 'italic' |
Conclusion
Use the font parameter in ttk.Entry constructor to customize font appearance. You can specify font family, size, and style using tuple, string, or dictionary format for flexible styling options.
