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

  • Graphics in Python with Cairo

    Build graphic elements into your Python programs with the Cairo graphics library. We'll show you how to draw an analog clock face that displays the current time.

  • Blender

    With a little help from Blender you can create your own 3D models – including animations. This article shows you how to assemble a partially automated virtual watch model with Blender and Python.

  • Use KDE's Digital Clock Widget as a World Clock
  • Programming Snapshot – Go Racing Game

    The fastest way through a curve on a racetrack is along the racing line. Instead of heading for Indianapolis, Mike Schilli trains his reflexes with a desktop application written in Go, just to be on the safe side.

  • DIY Alarm Clock

    A few electronic components, some code, and a hand-made wooden case make a fine retro-style bedside clock.

comments powered by Disqus