What is Label Widget?

The label is the Tkinter widget class and it is used to display text and images. Labels are widgets that the user does not manipulate/interact with it. It is only used to display some information or values.

Example

The following Python script creates a window containing one label widget with text “Hello Tkinter”.

You need to write following code in any text editor and then save it with .py extension. To execute the application type py LabelExample.py command in terminal.

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

label = Label(window, text="Hello Tkinter", font=("Arial Bold", 16))
label.grid(column=0, row=0)

window.mainloop()

Output

The above code produces the following output in windows operating system.

Explanation

Following line creates a Tkinter window

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

See explanation of Tkinter window – Tkinter top level window


The following block of code creates a label widget.

label = Label(window, text="Hello Tkinter", font=("Arial Bold", 16))
label.grid(column=0, row=0)

Properties

  • window – The container window.
  • text – Text to be displayed on the label widget.
  • font – Font family and size for text on the label.

Grid method sets the position of the label on parent form/widget.


Some label properties

Property NameDescription
anchorControls the position of the text (or image) within the label. Valid values: N, NE, E, SE, S, SW, W, NW, or CENTER. Default is CENTER.
background / bgSets the background color of widget, Default Is platform specific
bitmapBitmap to be displayed on the widget. If the image option is specified, this option is ignored silently.
borderwidth / bdWidth of label border. The default is system specific but usually 1 to 2 pixels.
compoundControls how text and images within a label are combined. By default, if an image or bitmap is given it will be drawn instead of text. When this option is set to CENTER, text is drawn on the image. Valid values: BOTTOM, LEFT, RIGHT, or TOP
cursorThe cursor to display when the mouse moves over the label. By default, the standard cursor is used.
disabledforegroundThe foreground color to use when the label is disabled or invalid. The default is system specific.
fontFont to use for labels. Labels can only contain single font text. The default is system specific.
foreground / fgThe color of the label used for text color and bitmap label. The default is system specific
heightThe height of the label. If text is displayed on the label, the size is indicated in text units. If an image is displayed on the label, the size is specified in pixels (or screen units). If you set the size to 0 or omit it, it is calculated based on the contents of the label.
imageImage to display on the widget. The value must be PhotoImage, BitmapImage, or compatible object. If specified, this overrides text and bitmap options.
padxHorizontal padding to add around the text. The default is 1 pixel.
padyVertical padding to add around the text. The default is 1 pixel.