Drawing and automating a clock with Cairo and GTK

Window of Opportunity

© Lead Image © Galina Peshkova, 123RF.com

© Lead Image © Galina Peshkova, 123RF.com

Article from Issue 297/2025
Author(s):

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

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
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.

Learn More

News