Python Tkinter Label Example – Programming Code Examples

Python Tkinter Label Example4 min read

Python Tkinter Label

We start our Tkinter tutorial with the simplest Tk (Tkinter) widget, i. a label. A label is a Tkinter class that can be used to represent text or an image. A label is a widget that the user can only view, but no interactions are possible.

There is hardly a book, tutorial or introduction to a programming language that does not start with the “Hello World” example. Also we traditionally start our introduction, but will change the edition slightly.

The following python script uses Tkinter to generate a window with the text “Hello Tkinter”.
Attention: Under Python3 you have to write Tkinter small, so “from tkinter import *”:

Call of the sample program

If you saved the script under the name, you can start it as follows:

If you start the command under Windows, the window looks like this:


The Tkinter module contains the Tk Toolkit and it always has to be imported. Note that the module is capitalized in the Python 2.x versions, while it is lowercase in Python 3.x. So you have to import it as “from tkinter import *”.

The Tkinter module must always be imported if you want to work with Tk / Tkinter. In our example we import everything into our namespace from Tkinter with the asterisk (“*”). This will save us typing.

To initialize Tkinter, we need to create a Tkinter root widget. This is done by calling Tk (). This widget provides the title bar and decorations provided by the window manager used. The root widget must be created before any other widgets are used. There can only be one root widget in each application.

The next line is about defining the label widget. The first parameter of the Label method contains the parent widget, in our case “root”. So our label widget is a child of the root widget. The keyword parameter “text” is assigned the string to be displayed:

The pack method is necessary to embed the label in the existing widgets:

The label is not displayed until we apply the mainloop method to the root widget:

The window created by our script will remain in the event loop until we close the window.

Python Tkinter Images in labels

As we mentioned earlier, labels can contain both text and images. The following example contains two labels, one with text and the other with an image.

Starting this script it looks like this:

You would like to print the text over the picture? No problem! One then uses only one label and sets the image and text options simultaneously. By default, however, the image is taken and not the text. To also output the text, you have to set the compound option. If you set compund to CENTER, the text is displayed centered over the image:


We can also move the image to the right side and center the text on the left side:

If the compound option is set to LEFT, RIGHT, TOP, or BOTTOM, the image will be set to the left, right, above, or below the text.

Python Colored labels in different fonts

For some widgets, such as the Label, Text or Canvas Widget, it is possible to specify certain fonts for display. You can set the option “font” accordingly. Fonts are some of different areas that are not platform independent.

The attribute fg can be used to output the text in a specific color. bg is the attribute that can be used to change the background color.

The result looks like this:

Python Dynamic content in a label widget

The following script shows an example in which a label is incremented dynamically by 1 until the stop button is pressed:


The result of the previous script:




Leave a Comment