Skip to content
← Back to Community
Tkinter Widgets Part 1: Label
Profile icon
Robotttttttt
Author(Chinese)

Tkinter Widgets (1): Label

When to use the Label widget?

The Label widget is used to display text or images on the screen. Label component can only display a single font. In addition, you can underline individual characters (For example, to indicate keyboard shortcuts).

Usage:

Label(master=None, **options)

Using the Label widget, you can specify what you want to display:

Text

from tkinter import * root = Tk() label = Label(root, text='Hello world') label.pack() root.mainloop()

image1

Image(Tai_ku.gif)

from tkinter import * root = Tk() photo = PhotoImage(file='tai-ku.gif') label = Label(root, image=photo) label.pack() root.mainloop()

image2

If you do not specify the size of the Label widget, the size of the Label is just enough to hold its content.

Of course, you can set the size of it through the height and width options: if you are displaying text, then these two options define the size of it in units of text units; if you are displaying bitmaps or images, then they define the label size in pixels.

You can set the foreground and background colors of it through the foreground (or fg) option and the background (or bg) option. You can also choose the font to display the text in it.

from tkinter import * root = Tk() label = Label(root, text='Hello', font=('Arial', 25), bg='blue', fg='green') label.pack() root.mainloop()

The Label widget can display multiple lines of text too. You can use '\n' or the wraplength option to achieve. When the text wraps, you can use the anchor option and justify option to make the text appear as you want:

from tkinter import * root = Tk() longtext = ''' This is an apple. And this is an orange. This is a new line. ''' label = Label(root, text=longtext, anchor='w', justify='left') label.pack() root.mainloop()

image3

It can display the Tkinter variables. The implication is that when the variable changes, the content displayed in It will also be automatically updated:

from tkinter import * root = Tk() var = StringVar() label = Label(root, textvariable=var) var.set('New File') label.pack() root.mainloop()

image4

You can use it to display the PhotoImage object and the BitmapImage object. When you do this, be sure to keep a reference to the image object to prevent it from being reclaimed by Python. You can use a global variable, or an attribute of an instance, or simply add an attribute to the instance:
Image(Earth.gif)

from tkinter import * root = Tk() photo = PhotoImage(file='earth.gif') label = Label(root, image=photo) label.pack() root.mainloop()

image5

Parameters:

master - Parent widget
*__ *options - Widget options: __

Voters
Profile icon
BCP
Profile icon
Robotttttttt
Comments
hotnewtop
Profile icon
Robotttttttt

Plz see me and see Author