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 increase the font size of a Text widget?
We can customize the Tkinter widget by modifying the value of its properties such as font-family, text-size, width, the height of the frame, etc. Tkinter Text widgets are generally used for accepting multiline user input. It is similar to a standard text widget.
To configure the text properties of a widget, we can use the font('font-family font-size font-style') attribute by defining its font-family, font-size, and the font style.
Basic Example
Here's how to create a Text widget with customized font size ?
# Import tkinter library
from tkinter import *
# Create an instance of Tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("750x250")
# Create a Text widget with custom font
text = Text(win, width=60, height=10, font=('Century Schoolbook', 20, 'italic'))
text.insert(INSERT, "Hello\nWelcome to TutorialsPoint.com!")
text.pack()
win.mainloop()
The output displays a text editor with customized font attributes ?
Different Font Sizes
You can easily change the font size by modifying the second parameter in the font tuple ?
from tkinter import *
win = Tk()
win.geometry("800x400")
# Small font size (12)
text1 = Text(win, width=50, height=3, font=('Arial', 12, 'normal'))
text1.insert(INSERT, "Small font size (12px)")
text1.pack(pady=10)
# Medium font size (16)
text2 = Text(win, width=50, height=3, font=('Arial', 16, 'normal'))
text2.insert(INSERT, "Medium font size (16px)")
text2.pack(pady=10)
# Large font size (24)
text3 = Text(win, width=50, height=3, font=('Arial', 24, 'normal'))
text3.insert(INSERT, "Large font size (24px)")
text3.pack(pady=10)
win.mainloop()
Font Configuration Options
| Parameter | Description | Example |
|---|---|---|
| Font Family | Name of the font | 'Arial', 'Times New Roman', 'Courier' |
| Font Size | Size in pixels | 10, 12, 16, 20, 24 |
| Font Style | Style attributes | 'normal', 'bold', 'italic' |
Using tkinter.font Module
For more advanced font control, you can use the tkinter.font module ?
from tkinter import *
from tkinter import font
win = Tk()
win.geometry("600x300")
# Create a custom font object
custom_font = font.Font(family="Helvetica", size=18, weight="bold")
# Apply the custom font to Text widget
text = Text(win, width=40, height=8, font=custom_font)
text.insert(INSERT, "Custom font using tkinter.font module\nFont size: 18px\nWeight: bold")
text.pack(pady=20)
win.mainloop()
Conclusion
To increase font size in Tkinter Text widgets, modify the second parameter in the font tuple: font=('Arial', 20, 'normal'). Use the tkinter.font module for advanced font customization and better control over text appearance.
