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 *”:
1 2 3 4 5 6 7 8 9 10 |
from tkinter import * root = Tk() w = Label(root, text="Hello Tkinter!") w.pack() root.mainloop() |
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:
1 2 3 |
python hello.py |
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.
1 2 3 |
from Tkinter import * |
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.
1 2 3 |
root = Tk() |
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:
1 2 3 |
w = label(root, text = "Hello Tkinter!") |
The pack method is necessary to embed the label in the existing widgets:
1 2 3 |
w.pack() |
The label is not displayed until we apply the mainloop method to the root widget:
1 2 3 |
root.mainloop() |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from tkinter import * root = Tk() logo = PhotoImage(file="../directory/images/sample.png") w1 = Label(root, image=logo).pack(side="right") explanation = """At present, only GIF and PPM/PGM formats are supported, but an interface exists to allow additional image file formats to be added easily.""" w2 = Label(root, justify=LEFT, padx = 10, text=explanation).pack(side="left") root.mainloop() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from tkinter import * root = Tk() logo = PhotoImage(file="../images/sample.png) explanation = """At present, only GIF and PPM/PGM formats are supported, but an interface exists to allow additional image file formats to be added easily.""" w = Label(root, compound = CENTER, text=explanation, image=logo, foreground="yellow").pack(side="right") root.mainloop() |
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:
1 2 3 4 5 6 7 8 |
w = Label(root, justify=LEFT, compound = LEFT, padx = 10, text=explanation, image=logo).pack(side="right") |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from tkinter import * root = Tk() Label(root, text="Red Text in Times Font", fg = "red", font = "Times").pack() Label(root, text="Green Text in Helvetica Font", fg = "light green", bg = "dark green", font = "Helvetica 16 bold italic").pack() Label(root, text="Blue Text in Verdana bold", fg = "blue", bg = "yellow", font = "Verdana 10 bold").pack() root.mainloop() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import tkinter as tk counter = 0 def counter_label(label): def count(): global counter counter += 1 label.config(text=str(counter)) label.after(1000, count) count() root = tk.Tk() root.title("Counting Seconds") label = tk.Label(root, fg="green") label.pack() counter_label(label) button = tk.Button(root, text='Stop', width=25, command=root.destroy) button.pack() root.mainloop() |
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.