Tkinter

Python Tkinter Label Example9 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 *”:

This code creates a simple Tkinter GUI with a single label that displays the text “Hello Tkinter!”.

The Tk class is used to create the main window of the application, and the Label class is used to create a widget that displays text. The pack method is used to add the label to the main window and adjust the layout of the window.

Finally, the mainloop function is called to start the Tkinter event loop and handle events for the main window. This will cause the program to wait for user input and update the GUI as needed.

Call of the sample program

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

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

Explanation

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.

This code creates a simple Tkinter GUI with an image and a label that displays text.

The Tk class is used to create the main window of the application, and the PhotoImage class is used to create an image object from a file. The Label class is used to create a widget that displays the image and text.

The pack method is used to add the widgets to the main window and adjust the layout of the window. The justify option is used to align the text in the label to the left, and the padx option is used to add padding to the left side of the label.

Finally, the mainloop function is called to start the Tkinter event loop and handle events for the main window. This will cause the program to wait for user input and update the GUI as needed.

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:

This code creates a simple Tkinter GUI with a label that displays text and an image.

The Tk class is used to create the main window of the application, and the PhotoImage class is used to create an image object from a file. The Label class is used to create a widget that displays the image and text.

The pack method is used to add the widget to the main window and adjust the layout of the window. The compound option is used to specify that the label should display the image and text together, and the foreground option is used to set the color of the text to yellow.

Finally, the mainloop function is called to start the Tkinter event loop and handle events for the main window. This will cause the program to wait for user input and update the GUI as needed.

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

This code creates a Label widget that displays text and an image on the left side of the widget.

The Label class is used to create a widget that displays the image and text. The justify option is used to align the text in the label to the left, and the compound option is used to specify that the label should display the image on the left side of the text. The padx option is used to add padding to the left side of the label.

The pack method is then used to add the widget to the main window and adjust the layout of the window.

This code will create a Label widget with the text and image displayed horizontally, with the image on the left and the text on the right. The widget will be aligned to the left of the window and will have some padding 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:

This code creates a simple Tkinter GUI with three labels that display text in different fonts and colors.

The Tk class is used to create the main window of the application, and the Label class is used to create widgets that display text.

The pack method is used to add the widgets to the main window and adjust the layout of the window. The fg option is used to set the color of the text, the bg option is used to set the background color of the label, and the font option is used to set the font of the text.

Finally, the mainloop function is called to start the Tkinter event loop and handle events for the main window. This will cause the program to wait for user input and update the GUI as needed.

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:

This code creates a simple Tkinter GUI with a label that displays a counter that increments every second and a button that stops the counter and closes the window when clicked.

The Tk class is used to create the main window of the application, and the Label class is used to create a widget that displays the counter. The Button class is used to create a button widget that stops the counter and closes the window when clicked.

The pack method is used to add the widgets to the main window and adjust the layout of the window.

The counter_label function is a nested function that increments the counter and updates the label every second using the after method. The count function is called initially and is then called again every 1000 milliseconds (1 second) using the after method.

Finally, the mainloop function is called to start the Tkinter event loop and handle events for the main window. This will cause the program to wait for user input and update the GUI as needed.

Leave a Comment