Tkinter Fonts Examples
Below, are the Tkinter Fonts code examples those are as follows.
Creating a Tkinter Window with a Custom Font
In this example, below code creates a Tkinter window and defines a normal Arial font. Then, it displays a label with the text “Normal Text” using the defined font in the window.
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk()
# Create fonts with different weights and slants
normal_font = tkFont.Font(family="Arial", size=12, weight=tkFont.NORMAL)
# Create labels using different font styles
label = tk.Label(root, text="Normal Text", font=normal_font)
label.pack()
root.mainloop()
Output
Creating a Tkinter Window with a Bold Label Using Helvetica Font
In this example, below code creates a Tkinter window with a bold “Hello, Tkinter!” label using the Helvetica font, size 12. It sets up the window, font style, and label, then displays it before entering the main event loop.
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk()
font = tkFont.Font(family="Helvetica", size=12, weight="bold")
label = tk.Label(root, text="Hello, Tkinter!", font=font)
label.pack()
root.mainloop()
Output
Styling Text with Italic Fonts in Tkinter
In this example, below code sets up a Tkinter window and creates a bold Helvetica font. Then, it displays a label with the text “Hello, Tkinter!” in the window, using the bold font.
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk()
font = tkFont.Font(family="Arial", size=10, slant="italic", underline=1)
label = tk.Label(root, text="Styled Text", font=font)
label.pack()
root.mainloop()
Output
Styling Text with Roman Fonts in Tkinter
In this example, below code creates Tkinter window and defines a font in Arial style with a Roman slant. Subsequently, it displays a label in the window with the text “Roman Text” formatted using the defined font.
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk()
# Create fonts with different weights and slants
roman_font = tkFont.Font(family="Arial", size=12, slant=tkFont.ROMAN)
# Create labels using font styles
label = tk.Label(root, text="Roman Text", font=roman_font)
label.pack()
root.mainloop()
Output
Tkinter Window with Resizable Text and Font Size Adjustment Buttons
In this example, below Python code creates a Tkinter window with a label displaying “Resizable Text” in Times font. It includes buttons to increase and decrease the font size by 2 points each time they’re clicked, ensuring the font size doesn’t drop below 8 points.
import tkinter as tk
import tkinter.font as tkFont
def increase_font_size():
font.config(size=font.actual()['size'] + 2)
def decrease_font_size():
font.config(size=max(8, font.actual()['size'] - 2)) # Ensure font size doesn't go below 8
root = tk.Tk()
font = tkFont.Font(family="Times", size=12)
label = tk.Label(root, text="Resizable Text", font=font)
label.pack()
increase_button = tk.Button(root, text="Increase Font Size", command=increase_font_size)
increase_button.pack()
decrease_button = tk.Button(root, text="Decrease Font Size", command=decrease_font_size)
decrease_button.pack()
root.mainloop()
Output
Enhancing Text Presentation with Tkinter Fonts
In Graphical User Interfaces (GUIs), how text is presented holds significant importance for user experience. Tkinter, known as the Primary GUI toolkit for Python, provides powerful capabilities for font management using its Tkinter Fonts module. This module enables developers to tailor the visual aspect of text within their applications, by improving legibility. In this article, we will see about Tkinter Fonts.