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
-
Wayland 1.24 Released with Fixes and New Features
Wayland continues to move forward, while X11 slowly vanishes into the shadows, and the latest release includes plenty of improvements.
-
Bugs Found in sudo
Two critical flaws allow users to gain access to root privileges.
-
Fedora Continues 32-Bit Support
In a move that should come as a relief to some portions of the Linux community, Fedora will continue supporting 32-bit architecture.
-
Linux Kernel 6.17 Drops bcachefs
After a clash over some late fixes and disagreements between bcachefs's lead developer and Linus Torvalds, bachefs is out.
-
ONLYOFFICE v9 Embraces AI
Like nearly all office suites on the market (except LibreOffice), ONLYOFFICE has decided to go the AI route.
-
Two Local Privilege Escalation Flaws Discovered in Linux
Qualys researchers have discovered two local privilege escalation vulnerabilities that allow hackers to gain root privileges on major Linux distributions.
-
New TUXEDO InfinityBook Pro Powered by AMD Ryzen AI 300
The TUXEDO InfinityBook Pro 14 Gen10 offers serious power that is ready for your business, development, or entertainment needs.
-
LibreOffice Tested as Possible Office 365 Alternative
Another major organization has decided to test the possibility of migrating from Microsoft's Office 365 to LibreOffice.
-
Linux Mint 20 Reaches EOL
With Linux Mint 20 at its end of life, the time has arrived to upgrade to Linux Mint 22.
-
TuxCare Announces Support for AlmaLinux 9.2
Thanks to TuxCare, AlmaLinux 9.2 (and soon version 9.6) now enjoys years of ongoing patching and compliance.