Tkinter Label - Width and Height
Tkinter Label - Width and Height
You can set the width of a Label widget in Tkinter to specific number of characters and the heigh of the Label widget to a specific number of lines.
To set the width of a Label widget to specific number of characters, pass the required number as integer value as argument for the width parameter in the Label() constructor.
width=20
To set the height of a Label widget to specific number of lines, pass the required number as integer value as argument for the height parameter in the Label() constructor.
height=3
In this tutorial, you will learn how to set the width and height of a Label widget in Tkinter, with examples.
Examples
1. Label of 20 characters wide and 3 lines height
In this example, we shall create a Label with a width of 20 characters, and a height of 3 lines.
We have set the background color, so that we can see the whole Label widget dimensions.
Program
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")
# Create a label widget with specific font
label = tk.Label(window, text="Hello World!", width=20, height=3, bg="lightgreen")
# Pack the label widget to display it
label.pack()
# Run the application
window.mainloop()
Output
Run on a MacOS.
2. Label of 30 characters wide and 5 lines height
In this example, we shall create a Label with a width of 30 characters, and a height of 5 lines.
Program
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")
# Create a label widget with specific font
label = tk.Label(window, text="Hello World!", width=30, height=5, bg="lightgreen")
# Pack the label widget to display it
label.pack()
# Run the application
window.mainloop()
Output
Run on a MacOS.
Summary
In this Python Tkinter tutorial, we learned how to set a specific width and height for a Label widget, with examples.