Tkinter Label

In this tutorial, I will teach you about the Tkinter label widget in python. we will also discuss the various options that we can use to customize the widget and some code examples for better understanding.

Tkinter label is the standard widget that can be used to display text and images inside the GUI application.

Tkinter Label Syntax

l = Label(window , **options)

Label Options

Here is the list of options that we can use to customize the Label.

activebackgroundSet the background color of the label when it’s active.
activeforegroundSet the text color of the label when it’s active.
anchorSet the location of the text or image inside the label. default is CENTER.
backgroundSet the background color of the label
borderwidthSet the border width of the label.
compoundUse this option to control how to display both text and image.
cursorSet the cursor when mouse is on label.
disabledforegroundSet the background color of the label when it’s disabled.
fontSet the font for the label text.
foregroundSet the text color of the label.
heightSet the height of the label.
highlightbackgroundSet the background color of the label when it’s on the focus.
imageUse this option to set the image.
justifySet the alignment of the label content. LEFT, RIGHT, CENTER(default)
padxSet the horizontal padding between the border and text.
padySet the vertical padding between the border and text.
stateSet the state of the label. use NORMAL(default), DISABLED.
textSet the text to display on the label.
textvariableUse this option to control the text on the label.
widthSet the width of the label.

Tkinter Label Examples :

For the better understanding of the concepts, let’s look at some code examples.

Example 1 : A Simple Label

In this example, we are displaying the simple label with the text “One Line Label” on the screen. here is code for that.

from tkinter import *

window = Tk() # Create a window

# Create a simple label inside window
label = Label(window,text = "One Line Label")
label.pack()

window.mainloop()

Output :

Tkinter label example 1 output

Example 2 : Multi-line Text Label

To display multi-line texts add ‘\n’ inside the string. here is the code for that.

from tkinter import *

window = Tk() # Create a window

# Create a simple label inside window
label = Label(window,text = "Line 1 \nLine 2")
label.pack()

window.mainloop()

Output :

Tkinter label example 2 output

Example 3 : Add Styles To Tkinter Labels

In this code example , we have used above discussed options to add some styles to labels.

from tkinter import *
from tkinter import ttk

window = Tk() # Create a window

ttk.Label(window,text = "TTK Label").pack() # Simple TTK label

Label(window,text="Red Text In Black",
    background = "black",foreground="red",
    font = "arial 15 bold").pack()

Label(window,text="White Text In green",
    background = "green",foreground="white",
    font = "verdana 15 bold",
    padx = 10 , pady = 10 ).pack()

window.mainloop()

Output :

Label example 3 output

Example 4 : Display Image Inside the Label

In this code example , we have shown how we can display a simple image inside the label.

from tkinter import *

window = Tk() # Create a window

# Load a Image
image = PhotoImage(file="dog.png")

# Set the image
Label(window, image=image).pack()

window.mainloop()

Output :

tkinter label example 4 : output

Example 5 : Display Text And Image

In this example, we have used compound option to displayed both image and text inside the label.

from tkinter import *

window = Tk() # Create a window

image = PhotoImage(file="dog.png")

# Add both text and image
Label(window,text = "This is line 1\nThis is line 2",compound = RIGHT, image=image).pack()

window.mainloop()

Output :

tkinter label example 5 output

Example 6 : Control The Text Of Tkinter Label

In this final example we have used ‘textvariable’ option to control the text content of the label.

from tkinter import *

window = Tk() # Create a window

#Control the label text using a stringVar
label_text = StringVar()
label_text.set("Counter : 0") # set the initial text
COUNTER = 0

def increase():
    # Function to increase the value of counter
    global COUNTER
    COUNTER += 1
    label_text.set(f"Counter : {COUNTER}")

# Create a label and set the text variable
Label(window,textvariable = label_text).pack()
Button(window,command = increase,text = "Increase").pack()


window.mainloop()

Output :

tkinter label example 6 output

On button press . value of counter will increase.

That’s wrap for this tutorial.

Leave a Reply