pythongtkcairopygobject

Detect missing python-gi-cairo in Gtk program with Gtk.DrawingArea


If you run a simple Gtk.DrawingArea example with python-gi-cairo uninstalled, there is an error on the console:

TypeError: Couldn't find foreign struct converter for 'cairo.Context'

Unfortunately no exception is thrown - and the draw handler doesn't get called (the error must happen just before this).

Is there any way I can detect this?

I'd like to prompt the user to install python-gi-cairo.


Solution

  • According to the GI documentation, you can check missing foreign module by

    import gi
    import cairo
    gi.require_foreign('cairo')
    gi.require_foreign('cairo', 'Surface')
    

    if python3-gi-cairo is not installed, this should raise an ImportError which you can capture and deal with it as you wish.