Skip to content
← Back to Community
Tkinter Widgets Part 1: Label
Profile icon

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).


Label(master=None, **options)

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


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



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


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()


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()


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:

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



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

Profile icon
Profile icon
Profile icon

Plz see me and see Author