The tkinter label widgets can be used to show text or an image to the screen. A label can only display text in a single font. The text can span multiple lines.

You can put any text in a label and you can have multiple labels in a window (just like any widget can be placed multiple times in a window).

Related course: Python Desktop Apps with Tkinter

Example

introduction

A label can be addded with just two lines of code. The first line defines the label and the text. The second line sets the two dimensional position:

1
2
text = Label(self, text="Just do it")
text.place(x=70,y=90)

You can change the font color or size of the label:

1
2
label1 = Label(master, text="Tkinter", fg="red")
label1 = Label(master, text="Helvetica", font=("Helvetica", 18))

tkinter label

tkinter label example

This example shows a label on the screen. It is the famous ג€œhello worldג€ program for tkinter, but we decided to change the text.

If you do not specify a size for the label widget, it will be made just large enough to fit the text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from tkinter import *

class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)

text = Label(self, text="Just do it")
text.place(x=70,y=90)
#text.pack()

root = Tk()
app = Window(root)
root.wm_title("Tkinter window")
root.geometry("200x200")
root.mainloop()

tkinter clock

The tkinter label is using the techinque of double buffering. This technique prevents flicking of the screen when updating it.

You could make say a clock that updates every second, but won't see any flickering. This technique is pretty standard now, we don't expect any flicking in gui windows.

A clock would simply add a timer function, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from tkinter import *
import time

class App(Frame):
def __init__(self,master=None):
Frame.__init__(self, master)
self.master = master
self.label = Label(text="", fg="Red", font=("Helvetica", 18))
self.label.place(x=50,y=80)
self.update_clock()

def update_clock(self):
now = time.strftime("%H:%M:%S")
self.label.configure(text=now)
self.after(1000, self.update_clock)

root = Tk()
app=App(root)
root.wm_title("Tkinter clock")
root.geometry("200x200")
root.after(1000, app.update_clock)
root.mainloop()

That would show this clock which updates automatically:

tkinter clock