Drawing and automating a clock with Cairo and GTK
Window of Opportunity

© Lead Image © Galina Peshkova, 123RF.com
Add graphics that automatically update. We show you how to build an analog clock widget with the Cairo and GTK libraries.
Last month, I covered drawing an analog clock face using the Cairo graphics library [1],[2]. While the programs in that article drew to image files, this article demonstrates how to program an analog clock drawn in a graphical user interface (GUI) window. I will also show how to make the clock automatically update, redrawing every second to simulate a ticking clock.
Because I have already covered the process of drawing a clock, this article focuses on the GUI aspect of the program. For this project, I will use the GTK [3] GUI library, because it interfaces well with Cairo and the Python language, both of which I used in the previous clock program. GTK is used from a Python program via the GObjectIntrospection [4] library; you need to ensure that you have Cairo, GTK, and GObjectIntrospection installed before you start programming this project. You will also need the interface between Cairo and GObjectIntrospection in order to use Cairo with GTK. On Debian, Ubuntu, and their derivative Linux distributions, the following command will ensure everything you need is installed:
sudo apt install python3-cairo python3-gi python3-gi-cairo gir1.2-gtk-3.0
[...]
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

News
-
KDE Linux Alpha Available for Daring Users
It's official, KDE Linux has arrived, but it's not quite ready for prime time.
-
AMD Initiates Graphics Driver Updates for Linux Kernel 6.18
This new AMD update focuses on power management, display handling, and hardware support for Radeon GPUs.
-
AerynOS Alpha Release Available
With a choice of several desktop environments, AerynOS 2025.08 is almost ready to be your next operating system.
-
AUR Repository Still Under DDoS Attack
Arch User Repository continues to be under a DDoS attack that has been going on for more than two weeks.
-
RingReaper Malware Poses Danger to Linux Systems
A new kind of malware exploits modern Linux kernels for I/O operations.
-
Happy Birthday, Linux
On August 25, Linux officially turns 34.
-
VirtualBox 7.2 Has Arrived
With early support for Linux kernel 6.17 and other new additions, VirtualBox 7.2 is a must-update for users.
-
Linux Mint 22.2 Beta Available for Testing
Some interesting new additions and improvements are coming to Linux Mint. Check out the Linux Mint 22.2 Beta to give it a test run.
-
Debian 13.0 Officially Released
After two years of development, the latest iteration of Debian is now available with plenty of under-the-hood improvements.
-
Upcoming Changes for MXLinux
MXLinux 25 has plenty in store to please all types of users.