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 | text = Label(self, text="Just do it") |
You can change the font color or size of the label:
1 | label1 = Label(master, text="Tkinter", fg="red") |
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 | from tkinter import * |
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 | from tkinter import * |
That would show this clock which updates automatically: