The Wayback Machine - https://web.archive.org/web/20240710191933/https://www.geeksforgeeks.org/python-tkinter-label/
Open In App

Python Tkinter – Label

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Tkinter Label is a widget that is used to implement display boxes where you can place text or images. The text displayed by this widget can be changed by the developer at any time you want. It is also used to perform tasks such as underlining the part of the text and spanning the text across multiple lines. It is important to note that a label can use only one font at a time to display text. To use a label, you just have to specify what to display in it (this can be text, a bitmap, or an image).

Note: For more reference, you can read our article,

  1. What is Widgets
  2. Python Tkinter Overview
  3. Python Tkinter Tutorial

Tkinter Label Widget Syntax

Syntax: Label ( master, option )

Parameters:

  • master: This represents the parent window
  • options: Below is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Tkinter Label Options

  • anchor: This option is used to control the positioning of the text if the widget has more space than required for the text. The default is anchor=CENTER, which centers the text in the available space.
  • bg: This option is used to set the normal background color displayed behind the label and indicator.
  • height:This option is used to set the vertical dimension of the new frame.
  • width:Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • bd:This option is used to set the size of the border around the indicator. Default bd value is set on 2 pixels.
  • font:If you are displaying text in the label (with the text or textvariable option), the font option is used to specify in what font that text in the label will be displayed.
  • cursor:It is used to specify what cursor to show when the mouse is moved over the label. The default is to use the standard cursor.
  • textvariable: As the name suggests it is associated with a Tkinter variable (usually a StringVar) with the label. If the variable is changed, the label text is updated.
  • bitmap:It is used to set the bitmap to the graphical object specified so that, the label can represent the graphics instead of text.
  • fg:The label clior, used for text and bitmap labels. The default is system specific. If you are displaying a bitmap, this is the clior that will appear at the position of the 1-bits in the bitmap.
  • image: This option is used to display a static image in the label widget.
  • padx:This option is used to add extra spaces between left and right of the text within the label.The default value for this option is 1.
  • pady:This option is used to add extra spaces between top and bottom of the text within the label.The default value for this option is 1.
  • justify:This option is used to define how to align multiple lines of text. Use LEFT, RIGHT, or CENTER as its values. Note that to position the text inside the widget, use the anchor option. Default value for justify is CENTER.
  • relief: This option is used to specify appearance of a decorative border around the label. The default value for this option is FLAT.
  • underline:This
  • wraplength:Instead of having only one line as the label text it can be broken into to the number of lines where each line has the number of characters specified to this option.

Label Widget in Tkinter Example

In this example, below Python code creates a Tkinter GUI window with a labeled text “Hello, World!”. The label is styled with specific attributes such as font, color, and dimensions, and it’s positioned at the center with a raised border. Finally, the main event loop is started to display the GUI window until the user interacts with it.

Python3
import tkinter as tk

# Create the main window
root = tk.Tk()
root.geometry("400x250")  # Set window size
root.title("Welcome to My App")  # Set window title

# Create a StringVar to associate with the label
text_var = tk.StringVar()
text_var.set("Hello, World!")

# Create the label widget with all options
label = tk.Label(root, 
                 textvariable=text_var, 
                 anchor=tk.CENTER,       
                 bg="lightblue",      
                 height=3,              
                 width=30,              
                 bd=3,                  
                 font=("Arial", 16, "bold"), 
                 cursor="hand2",   
                 fg="red",             
                 padx=15,               
                 pady=15,                
                 justify=tk.CENTER,    
                 relief=tk.RAISED,     
                 underline=0,           
                 wraplength=250         
                )

# Pack the label into the window
label.pack(pady=20)  # Add some padding to the top


# Run the main event loop
root.mainloop()

Output

ko

Python Tkiner – Label




Previous Article
Next Article

Similar Reads

Change the label size and tick label size of colorbar using Matplotlib in Python
In this article, we will learn how to change the label size and tick label size of colorbar in Matplotlib using Python. Labels are a kind of assigning name that can be applied to any node in the graph. They are a name only and so labels are either present or absent. To properly label a graph, helps to identify the x-axis and y-axis. Each tick mark
2 min read
Add Shadow in Tkinter Label in Python
Tkinter is the standard GUI library for Python and is included with most Python installations. It provides a number of widgets and tools for creating graphical user interfaces (GUIs) in Python. One of the most common widgets in Tkinter is the Label widget, which is used to display text and images. In this article, we will look at how to add a shado
4 min read
PyQt5 – How to hide label | label.setHidden method
In this article, we will see how we can hide the Label in the PyQt5 application. A label is a graphical control element which displays text on a form. It is usually a static control; having no interactivity. A label is generally used to identify a nearby text box or another widget. In order to hide the label we use setHidden() method, this method a
2 min read
PyQt5 – How to change size of the Label | label.resize method
While designing any GUI(Graphical User Interface) application we create labels that provide information, but sometimes there might be a case when the label size is not appropriate for the content then need of resizing of label arises. In this tutorial, we will see how to change the size of the label in PyQt5. For this we will use resize() method. S
2 min read
PyQt5 Scrollable Label - Setting tool tip to the label part
In this article we will see how we can set tool tip to the label part of scroll label, when know we can make scrollable label with the help of inheriting a scroll class and making label in it, but when we set tool tip to the class object tool tip is set to the whole widget i.e label and the scroll bar as well. In order to add tool tip the label par
3 min read
PyQt5 Scrollable Label - Getting tool tip text of the label part
In this article we will see how we can get tool tip text of the label part of scrollable label, when know we can make scrollable label with the help of inheriting a scroll class and making label in it, but when we set tool tip to the class object tooltip is set to the whole widget i.e label and the scroll bar as well. In order to add tool tip the l
3 min read
PyQt5 Scrollable Label – Setting tool tip duration to label part
In this article we will see how we can set tooltip duration to the label part of scroll label, when know we can make scrollable label with the help of inheriting a scroll class and making label in it, but when we set tooltip to the class object tooltip is set to the whole widget i.e label and the scroll bar as well. In order to add tooltip and its
3 min read
PyQt5 Scrollable Label – Getting tool tip duration of the label part
In this article we will see how we can get the tooltip duration of the label part of scrollable label, when know we can make scrollable label with the help of inheriting a scroll class and making label in it, but when we set tooltip to the class object tooltip is set to the whole widget i.e label and the scroll bar as well. In order to get tooltip
3 min read
PyQt5 Label – Checking if label is window type
In this article we will see how we can check if the label is window type. Window type is the property which tells if the object is window type or not. In order to do this we use isWindowType method Syntax : label.isWindowType() Argument : It takes no argument Return : It returns bool Below is the implementation # importing libraries from PyQt5.QtWi
2 min read
PyQt5 Label – Checking if label is widget type
In this article we will see how we can check if the label is widget type. Widget type is the property which tells if the object is widget or not. In order to do this we use isWidgetType method Syntax : label.isWidgetType() Argument : It takes no argument Return : It returns bool Below is the implementation # importing libraries from PyQt5.QtWidgets
2 min read
three90RightbarBannerImg