wxWidgets / Phoenix

wxPython's Project Phoenix. A new implementation of wxPython, better, stronger, faster than he was before.
http://wxpython.org/
2.25k stars 512 forks source link

Difficulties with PangoCairo #247

Open manns opened 7 years ago

manns commented 7 years ago

I am trying to create a PangoCairo context from a Cairo context. I am using Python 3.5.3 on Debian sid, wxPython checkout a92cef7bab4758680c2d8225dad20cd465c045b0 with Subproject commit dc7acca2cd4c87fb2541df17d8b4d80e0f607cc8 The package python3-gi-cairo is installed.

import wx
import wx.lib.wxcairo
import gi
gi.require_version('PangoCairo', '1.0')
from gi.repository import PangoCairo

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        mdc = wx.MemoryDC()
        bmp = wx.Bitmap(10, 10)
        mdc.SelectObject(bmp)

        context = wx.lib.wxcairo.ContextFromDC(mdc)
        pangocairo_context = PangoCairo.create_context(context)

app = wx.App(redirect=False)
top = Frame()

top.Show()
app.MainLoop()

However, I get the following error message:

$ python3 test_cairo.py 
Gtk-Message: (for origin information, set GTK_DEBUG): failed to retrieve property `gtk-primary-button-warps-slider' of type `gboolean' from rc file value "((GString*) 0x563022e935a0)" of type `gboolean'
Traceback (most recent call last):
  File "test_cairo.py", line 20, in <module>
    top = Frame()
  File "test_cairo.py", line 16, in __init__
    pangocairo_context = PangoCairo.create_context(context)
KeyError: 'could not find foreign type Context'
/home/mn/prog/Phoenix/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC
/home/mn/prog/Phoenix/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC
/home/mn/prog/Phoenix/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC
/home/mn/prog/Phoenix/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC

I expected that line 16 would provide a PangoCairo context.

swt2c commented 7 years ago

Your code works fine for me on Fedora. I'll try to fire up my sid VM and see if I can figure out how to make it work there. It sounds like maybe a missing dependency.

manns commented 7 years ago

I just have recompiled Phoenix using gtk3 on Debian sid. Unfortunately, the behavior stays the same.

ausk commented 7 years ago

After update to wxPython (4.0.0a3.dev3059+4a5c5d9), it works fine on my ubuntu 16.04LTS。 Thks for maintaining!

================================================================== On Ubuntu 16.04 LTS, Python3.5 + wxPhonix 4.0.0a1, I get the same error using the same code. Maybe it is because of not well compatible with the gtk?

Here is the wrong info:

$ python3 test.py 
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    top = Frame()
  File "test.py", line 16, in __init__
    pangocairo_context = PangoCairo.create_context(context)
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    top = Frame()
  File "test.py", line 16, in __init__
    pangocairo_context = PangoCairo.create_context(context)
KeyError: 'could not find foreign type Context'
/home/auss/Programs/wxPythonPhonix/wxPythonPhonix4.0.0a1/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC
/home/auss/Programs/wxPythonPhonix/wxPythonPhonix4.0.0a1/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC
/home/auss/Programs/wxPythonPhonix/wxPythonPhonix4.0.0a1/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC
/home/auss/Programs/wxPythonPhonix/wxPythonPhonix4.0.0a1/ext/wxWidgets/src/gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC

This is the gtk info:

$ dpkg -l | grep gtk

....
charliegreen commented 7 years ago

I don't believe this issue is entirely with PangoCairo.

#!/usr/bin/env python3

import wx

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None)

    bitmap = wx.Bitmap(400, 400)
    static = wx.StaticBitmap(frame, label=bitmap)

    frame.Show()

    dc = wx.MemoryDC(bitmap)

    app.MainLoop()

With the above code, the window opens just fine, but when I close it, I get the same four gtk/dcclient.cpp(250): assert "Assert failure" failed in wxFreePoolGC(): Wrong GC errors. If I comment out the wx.MemoryDC line, I get no errors.

(on debian stretch, wx.version() "4.0.0a3 gtk2 (phoenix)", python 3.5.3)

swt2c commented 7 years ago

Very strange. I've just tried the original test code and it works fine for me also on Debian sid.

RobinD42 commented 7 years ago

I expect that is has more to do with when the MemoryDC is created and also the asynchronous nature of X windows. If you move the line creating the MemoryDC into a function that is called later via CallAfter or some event handler then the assert warnings go away.

manns commented 7 years ago

The original test code still does not work for me in Debian sid. I pulled Phoenix from git last Friday, compiled it using Python3 and set the PYTHONPATH so that Phoenix gets invoked.

@swt2c Could we compare setups?


~$ dpkg -l | grep gtk
ii  albatross-gtk-theme                           1.7.4-1                           all          dark and light GTK+ theme from the Shimmer Project
ii  blackbird-gtk-theme                           0.4+20160704~ab4a30e-1            all          dark GTK+ theme from the Shimmer Project
ii  bluebird-gtk-theme                            1.2-1                             all          blue GTK+ theme from the Shimmer Project
ii  deluge-gtk                                    1.3.15-1                          all          bittorrent client written in Python/PyGTK (GTK+ ui)
ii  evince-gtk                                    3.22.1-4                          amd64        dummy package for the transition from evince-gtk to evince
ii  gir1.2-gtk-2.0                                2.24.31-2                         amd64        GTK+ graphical user interface library -- gir bindings
ii  gir1.2-gtk-3.0:amd64                          3.22.16-1                         amd64        GTK+ graphical user interface library -- gir bindings
ii  gir1.2-gtksource-3.0:amd64                    3.22.2-1                          amd64        gir files for the GTK+ syntax highlighting widget
ii  gir1.2-javascriptcoregtk-3.0:amd64            2.4.11-3                          amd64        JavaScript engine library from WebKitGTK+ - GObject introspection data
ii  gir1.2-javascriptcoregtk-4.0:amd64            2.16.5-1                          amd64        JavaScript engine library from WebKitGTK+ - GObject introspection data
ii  greybird-gtk-theme                            3.22.2-1                          all          grey GTK+ theme from the Shimmer Project
ii  gtk-update-icon-cache                         3.22.16-1                         amd64        icon theme caching utility
ii  gtk2-engines:amd64                            1:2.20.2-3                        amd64        theme engines for GTK+ 2.x
ii  gtk2-engines-murrine:amd64                    0.98.1.1-6                        amd64        cairo-based gtk+-2.0 theme engine
ii  gtk2-engines-pixbuf:amd64                     2.24.31-2                         amd64        pixbuf-based theme for GTK+ 2.x
ii  gtk2-engines-xfce                             3.2.0-2                           amd64        GTK+-2.0 theme engine for Xfce
ii  gtk3-engines-breeze                           5.8.7-1                           amd64        GTK theme built to match KDE's Breeze
ii  gtkhash                                       1.0-2                             amd64        GTK+ utility for computing checksums and more
ii  kde-config-gtk-style                          4:5.8.6-1                         amd64        KDE configuration module for GTK+ 2.x and GTK+ 3.x styles selection
ii  libcanberra-gtk-module:amd64                  0.30-3                            amd64        translates GTK+ widgets signals to event sounds
ii  libcanberra-gtk0:amd64                        0.30-3                            amd64        GTK+ helper for playing widget event sounds with libcanberra
ii  libcanberra-gtk3-0:amd64                      0.30-3                            amd64        GTK+ 3.0 helper for playing widget event sounds with libcanberra
ii  libcanberra-gtk3-module:amd64                 0.30-3                            amd64        translates GTK3 widgets signals to event sounds
ii  libchamplain-gtk-0.12-0:amd64                 0.12.15-1                         amd64        Gtk+ widget to display maps
ii  libclutter-gtk-1.0-0:amd64                    1.8.2-2                           amd64        Open GL based interactive canvas library GTK+ widget
ii  libdbusmenu-gtk3-4:amd64                      12.10.2-2                         amd64        library for passing menus over DBus - GTK+ version
ii  libdbusmenu-gtk4:amd64                        12.10.2-2                         amd64        library for passing menus over DBus - GTK+ version
ii  libfm-gtk-data                                1.2.5-1                           all          file management support (GTK+ library common data)
ii  libfm-gtk4:amd64                              1.2.5-1                           amd64        file management support (GTK+ 2.0 GUI library)
ii  libgnome-autoar-gtk-0-0:amd64                 0.1.1-4+b1                        amd64        GTK+ widgets for the GNOME Autoar library
ii  libgtk-3-0:amd64                              3.22.16-1                         amd64        GTK+ graphical user interface library
ii  libgtk-3-bin                                  3.22.16-1                         amd64        programs for the GTK+ graphical user interface library
ii  libgtk-3-common                               3.22.16-1                         all          common files for the GTK+ graphical user interface library
ii  libgtk-3-dev:amd64                            3.22.16-1                         amd64        development files for the GTK+ library
ii  libgtk2-perl                                  2:1.24992-1                       amd64        Perl interface to the 2.x series of the Gimp Toolkit library
ii  libgtk2.0-0:amd64                             2.24.31-2                         amd64        GTK+ graphical user interface library
ii  libgtk2.0-bin                                 2.24.31-2                         amd64        programs for the GTK+ graphical user interface library
ii  libgtk2.0-common                              2.24.31-2                         all          common files for the GTK+ graphical user interface library
ii  libgtk2.0-dev                                 2.24.31-2                         amd64        development files for the GTK+ library
ii  libgtk3.0-cil                                 2.99.3-2+b1                       amd64        CLI binding for the GTK+ 3.0 toolkit
rc  libgtkglext1                                  1.2.0-3.2                         amd64        OpenGL Extension to GTK+ (shared libraries)
ii  libgtkmm-2.4-1v5:amd64                        1:2.24.5-1                        amd64        C++ wrappers for GTK+ (shared libraries)
ii  libgtkmm-3.0-1v5:amd64                        3.22.1-1                          amd64        C++ wrappers for GTK+ (shared libraries)
ii  libgtksourceview-3.0-1:amd64                  3.22.2-1                          amd64        shared libraries for the GTK+ syntax highlighting widget
ii  libgtksourceview-3.0-common                   3.22.2-1                          all          common files for the GTK+ syntax highlighting widget
ii  libgtksourceview2.0-0:amd64                   2.10.5-3                          amd64        shared libraries for the GTK+ syntax highlighting widget
ii  libgtksourceview2.0-common                    2.10.5-3                          all          common files for the GTK+ syntax highlighting widget
ii  libgtkspell0                                  2.0.16-1.1                        amd64        a spell-checking addon for GTK's TextView widget
ii  libgtkspell3-3-0:amd64                        3.0.9-1                           amd64        spell-checking addon for GTK+'s TextView widget
ii  libjavascriptcoregtk-1.0-0:amd64              2.4.11-3                          amd64        JavaScript engine library from WebKitGTK+
ii  libjavascriptcoregtk-1.0-dev:amd64            2.4.11-3                          amd64        JavaScript engine library from WebKitGTK+ - development files
ii  libjavascriptcoregtk-3.0-0:amd64              2.4.11-3                          amd64        JavaScript engine library from WebKitGTK+
ii  libjavascriptcoregtk-3.0-dev:amd64            2.4.11-3                          amd64        JavaScript engine library from WebKitGTK+ - development files
ii  libjavascriptcoregtk-4.0-18:amd64             2.16.5-1                          amd64        JavaScript engine library from WebKitGTK+
ii  libnm-gtk-common                              1.4.2-1                           all          library for wireless and mobile dialogs - common files
ii  libnm-gtk0:amd64                              1.8.2-1                           amd64        library for wireless and mobile dialogs (libnm-glib version)
ii  libreoffice-gtk2                              1:5.3.4-4                         amd64        office productivity suite -- GTK+ 2 integration
ii  libreoffice-gtk3                              1:5.3.4-4                         amd64        office productivity suite -- GTK+ 3 integration
ii  libtopmenu-client-gtk3-0                      0.3-1                             amd64        Topmenu GTK3 (shared library¸ client part)
ii  libwebkit2gtk-4.0-37:amd64                    2.16.5-1                          amd64        Web content engine library for GTK+
ii  libwebkit2gtk-4.0-37-gtk2:amd64               2.16.5-1                          amd64        Web content engine library for GTK+ - GTK+2 plugin process
ii  libwebkitgtk-1.0-0:amd64                      2.4.11-3                          amd64        Web content engine library for GTK+
ii  libwebkitgtk-3.0-0:amd64                      2.4.11-3                          amd64        Web content engine library for GTK+
ii  libwebkitgtk-3.0-dev:amd64                    2.4.11-3                          amd64        Web content engine library for GTK+ - development files
ii  libwebkitgtk-dev:amd64                        2.4.11-3                          amd64        Web content engine library for GTK+ - development files
ii  libwebkitgtk3.0-cil                           2.0.0+git20151221-3               amd64        CLI bindings for WebKitGTK+ 3.0 using GObject Introspection
ii  libwebkitgtk3.0-cil-dev                       2.0.0+git20151221-3               amd64        CLI bindings for WebKitGTK+ 3.0 using GObject Introspection - development
ii  libwxgtk-media3.0-0v5:amd64                   3.0.2+dfsg-4                      amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ media library runtime)
ii  libwxgtk-media3.0-dev                         3.0.2+dfsg-4                      amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ media library development)
ii  libwxgtk-webview3.0-0v5:amd64                 3.0.2+dfsg-4                      amd64        wxWidgets Cross-platform C++ GUI toolkit (webview library runtime)
ii  libwxgtk-webview3.0-dev                       3.0.2+dfsg-4                      amd64        wxWidgets Cross-platform C++ GUI toolkit (webview library development)
ii  libwxgtk3.0-0v5:amd64                         3.0.2+dfsg-4                      amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ runtime)
ii  libwxgtk3.0-dev                               3.0.2+dfsg-4                      amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ development)
ii  lightdm-gtk-greeter                           2.0.2-1                           amd64        simple display manager (GTK+ greeter)
ii  lxhotkey-gtk                                  0.1.0-1+b1                        amd64        LXHotkey keyboard shortcuts configurator (GTK+ GUI plugin)
ii  murrine-themes                                0.98.11                           all          themes for gtk2 murrine engine
ii  pinentry-gtk2                                 1.0.0-2                           amd64        GTK+-2-based PIN or pass-phrase entry dialog for GnuPG
ii  python-gtk2                                   2.24.0-5.1                        amd64        Python bindings for the GTK+ widget set
ii  python-wxgtk-media3.0                         3.0.2.0+dfsg-4                    amd64        Python interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.media)
ii  python-wxgtk-webview3.0                       3.0.2.0+dfsg-4                    amd64        Python interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.html2)
ii  python-wxgtk3.0                               3.0.2.0+dfsg-4                    amd64        Python interface to the wxWidgets Cross-platform C++ GUI toolkit
ii  python-wxgtk3.0-dev                           3.0.2.0+dfsg-4                    all          Development files for wxPython
ii  qt5-gtk-platformtheme:amd64                   5.7.1+dfsg-4                      amd64        Qt 5 GTK+ 3 platform theme
ii  thunar-gtkhash                                1.0-2                             amd64        thunar extension for computing checksums and more using gtkhash
rc  wicd-gtk                                      1.7.4+tb2-2                       all          wired and wireless network manager - GTK+ client
swt2c commented 6 years ago

Here you go, but I don't think the gtk packages are the problem here:

talbert@debian-unstable:~$ dpkg -l | grep gtk
ii  gir1.2-gtk-2.0                        2.24.31-2                            amd64        GTK+ graphical user interface library -- gir bindings
ii  gir1.2-gtk-3.0:amd64                  3.22.16-1                            amd64        GTK+ graphical user interface library -- gir bindings
ii  gir1.2-gtkclutter-1.0:amd64           1.8.2-2                              amd64        GObject introspection data for the GTK+ Clutter library
ii  gir1.2-gtksource-3.0:amd64            3.22.2-1                             amd64        gir files for the GTK+ syntax highlighting widget
ii  gir1.2-javascriptcoregtk-4.0:amd64    2.16.5-1                             amd64        JavaScript engine library from WebKitGTK+ - GObject introspection data
ii  gir1.2-nmgtk-1.0:amd64                1.8.2-1                              amd64        GObject introspection data for libnm-gtk
ii  gtk-update-icon-cache                 3.22.16-1                            amd64        icon theme caching utility
ii  gtk2-engines-pixbuf:amd64             2.24.31-2                            amd64        pixbuf-based theme for GTK+ 2.x
ii  libcanberra-gtk-module:amd64          0.30-3                               amd64        translates GTK+ widgets signals to event sounds
ii  libcanberra-gtk0:amd64                0.30-3                               amd64        GTK+ helper for playing widget event sounds with libcanberra
ii  libcanberra-gtk3-0:amd64              0.30-3                               amd64        GTK+ 3.0 helper for playing widget event sounds with libcanberra
ii  libcanberra-gtk3-module:amd64         0.30-3                               amd64        translates GTK3 widgets signals to event sounds
ii  libcaribou-gtk-module:amd64           0.4.21-1+b1                          amd64        Configurable on screen keyboard with scanning mode (GTK+ 2.0 module)
ii  libcaribou-gtk3-module:amd64          0.4.21-1+b1                          amd64        Configurable on screen keyboard with scanning mode (GTK+ 3.0 module)
ii  libchamplain-gtk-0.12-0:amd64         0.12.15-1                            amd64        Gtk+ widget to display maps
rc  libcheese-gtk23:amd64                 3.16.1-1                             amd64        tool to take pictures and videos from your webcam - widgets
ii  libcheese-gtk25:amd64                 3.22.1-1+b1                          amd64        tool to take pictures and videos from your webcam - widgets
ii  libclutter-gtk-1.0-0:amd64            1.8.2-2                              amd64        Open GL based interactive canvas library GTK+ widget
ii  libcolord-gtk1:amd64                  0.1.26-1.1                           amd64        GTK+ convenience library for interacting with colord
ii  libgnome-autoar-gtk-0-0:amd64         0.1.1-4+b1                           amd64        GTK+ widgets for the GNOME Autoar library
ii  libgtk-3-0:amd64                      3.22.16-1                            amd64        GTK+ graphical user interface library
ii  libgtk-3-bin                          3.22.16-1                            amd64        programs for the GTK+ graphical user interface library
ii  libgtk-3-common                       3.22.16-1                            all          common files for the GTK+ graphical user interface library
ii  libgtk-3-dev:amd64                    3.22.16-1                            amd64        development files for the GTK+ library
ii  libgtk2-perl                          2:1.24992-1                          amd64        Perl interface to the 2.x series of the Gimp Toolkit library
ii  libgtk2.0-0:amd64                     2.24.31-2                            amd64        GTK+ graphical user interface library
ii  libgtk2.0-bin                         2.24.31-2                            amd64        programs for the GTK+ graphical user interface library
ii  libgtk2.0-common                      2.24.31-2                            all          common files for the GTK+ graphical user interface library
ii  libgtk2.0-dev                         2.24.31-2                            amd64        development files for the GTK+ library
rc  libgtkglext1                          1.2.0-3.2                            amd64        OpenGL Extension to GTK+ (shared libraries)
rc  libgtkhtml-4.0-0                      4.8.5-1                              amd64        HTML rendering/editing library - runtime files
rc  libgtkhtml-editor-4.0-0               4.8.5-1                              amd64        HTML rendering/editing library - editor widget
rc  libgtkmm-3.0-1:amd64                  3.14.0-1                             amd64        C++ wrappers for GTK+ (shared libraries)
ii  libgtkmm-3.0-1v5:amd64                3.22.0-1                             amd64        C++ wrappers for GTK+ (shared libraries)
ii  libgtksourceview-3.0-1:amd64          3.22.2-1                             amd64        shared libraries for the GTK+ syntax highlighting widget
ii  libgtksourceview-3.0-common           3.22.2-1                             all          common files for the GTK+ syntax highlighting widget
ii  libgtkspell3-3-0:amd64                3.0.9-1                              amd64        spell-checking addon for GTK+'s TextView widget
ii  libjavascriptcoregtk-1.0-0:amd64      2.4.11-3                             amd64        JavaScript engine library from WebKitGTK+
ii  libjavascriptcoregtk-4.0-18:amd64     2.16.5-1                             amd64        JavaScript engine library from WebKitGTK+
ii  libnm-gtk0:amd64                      1.8.2-1                              amd64        library for wireless and mobile dialogs (libnm-glib version)
rc  libseed-gtk3-0                        3.8.1-2                              amd64        GObject JavaScript bindings for the webkit engine - Runtime
ii  libwebkit2gtk-4.0-37:amd64            2.16.5-1                             amd64        Web content engine library for GTK+
ii  libwebkit2gtk-4.0-37-gtk2:amd64       2.16.5-1                             amd64        Web content engine library for GTK+ - GTK+2 plugin process
ii  libwebkitgtk-1.0-0:amd64              2.4.11-3                             amd64        Web content engine library for GTK+
ii  libwxgtk-media3.0-0v5:amd64           3.0.2+dfsg-4                         amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ media library runtime)
ii  libwxgtk-media3.0-dev                 3.0.2+dfsg-4                         amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ media library development)
ii  libwxgtk-webview3.0-0v5:amd64         3.0.2+dfsg-4                         amd64        wxWidgets Cross-platform C++ GUI toolkit (webview library runtime)
ii  libwxgtk-webview3.0-dev               3.0.2+dfsg-4                         amd64        wxWidgets Cross-platform C++ GUI toolkit (webview library development)
rc  libwxgtk2.8-0:amd64                   2.8.12.1+dfsg2-2                     amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ runtime)
rc  libwxgtk3.0-0:amd64                   3.0.2-1+b1                           amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ runtime)
ii  libwxgtk3.0-0v5:amd64                 3.0.2+dfsg-4                         amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ runtime)
ii  libwxgtk3.0-dev                       3.0.2+dfsg-4                         amd64        wxWidgets Cross-platform C++ GUI toolkit (GTK+ development)
ii  pinentry-gtk2                         1.0.0-2                              amd64        GTK+-2-based PIN or pass-phrase entry dialog for GnuPG
ii  python-wxgtk-webview3.0               3.0.2.0+dfsg-4                       amd64        Python interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.html2)
ii  python-wxgtk3.0                       3.0.2.0+dfsg-4                       amd64        Python interface to the wxWidgets Cross-platform C++ GUI toolkit
ii  python3-wx-phoenix-gtk3.0             3.0.3~dev2875+9b743cf-1              amd64        Python interface to the wxWidgets Cross-platform C++ GUI toolkit
ii  software-properties-gtk               0.96.20.2-1                          all          manage the repositories that you install software from (gtk)

How about your cairo packages?

talbert@debian-unstable:~$ dpkg -l | grep cairo
ii  libcairo-gobject2:amd64               1.14.8-1                             amd64        Cairo 2D vector graphics library (GObject library)
ii  libcairo-perl                         1.106-2                              amd64        Perl interface to the Cairo graphics library
ii  libcairo-script-interpreter2:amd64    1.14.8-1                             amd64        Cairo 2D vector graphics library (script interpreter)
ii  libcairo2:amd64                       1.14.8-1                             amd64        Cairo 2D vector graphics library
ii  libcairo2-dev                         1.14.8-1                             amd64        Development files for the Cairo 2D graphics library
rc  libcairomm-1.0-1                      1.10.0-1.1                           amd64        C++ wrappers for Cairo (shared libraries)
ii  libcairomm-1.0-1v5:amd64              1.12.0-1+b1                          amd64        C++ wrappers for Cairo (shared libraries)
ii  libpangocairo-1.0-0:amd64             1.40.5-1                             amd64        Layout and rendering of internationalized text
ii  libpixman-1-0:amd64                   0.34.0-1                             amd64        pixel-manipulation library for X and cairo
ii  libpixman-1-dev                       0.34.0-1                             amd64        pixel-manipulation library for X and cairo (development files)
ii  python-cairocffi                      0.7.2-2                              all          cffi-based cairo bindings for Python
ii  python-cairosvg                       1.0.20-1                             all          SVG to PDF/PS/PNG converter based on Cairo
ii  python3-cairo                         1.10.0+dfsg-5+b3                     amd64        Python 3 bindings for the Cairo vector graphics library
ii  python3-gi-cairo                      3.22.0-2                             amd64        Python 3 Cairo bindings for the GObject library

Are you still getting the same errors?

manns commented 6 years ago

OK, here are my cairo packages:


$ dpkg -l | grep cairo
ii  gtk2-engines-murrine:amd64                    0.98.1.1-6                        amd64        cairo-based gtk+-2.0 theme engine
ii  libcairo-gobject2:amd64                       1.14.10-1                         amd64        Cairo 2D vector graphics library (GObject library)
ii  libcairo-perl                                 1.106-2                           amd64        Perl interface to the Cairo graphics library
ii  libcairo-script-interpreter2:amd64            1.14.10-1                         amd64        Cairo 2D vector graphics library (script interpreter)
ii  libcairo1.10-cil                              2.99.3-2+b1                       amd64        CLI binding for Cairo
ii  libcairo2:amd64                               1.14.10-1                         amd64        Cairo 2D vector graphics library
ii  libcairo2:i386                                1.14.10-1                         i386         Cairo 2D vector graphics library
ii  libcairo2-dev                                 1.14.10-1                         amd64        Development files for the Cairo 2D graphics library
ii  libcairomm-1.0-1v5:amd64                      1.12.2-1                          amd64        C++ wrappers for Cairo (shared libraries)
ii  libpangocairo-1.0-0:amd64                     1.40.6-1                          amd64        Layout and rendering of internationalized text
ii  libpixman-1-0:amd64                           0.34.0-1                          amd64        pixel-manipulation library for X and cairo
ii  libpixman-1-0:i386                            0.34.0-1                          i386         pixel-manipulation library for X and cairo
ii  libpixman-1-dev                               0.34.0-1                          amd64        pixel-manipulation library for X and cairo (development files)
ii  python-cairo                                  1.8.8-2.2                         amd64        Python bindings for the Cairo vector graphics library
ii  python-cairo-dev                              1.8.8-2.2                         all          Python cairo bindings: development files
ii  python-cairocffi                              0.7.2-2.1                         all          cffi-based cairo bindings for Python
ii  python-cairosvg                               1.0.20-1                          all          SVG to PDF/PS/PNG converter based on Cairo
ii  python-gi-cairo                               3.22.0-2+b1                       amd64        Python Cairo bindings for the GObject library
ii  python3-cairo                                 1.10.0+dfsg-5+b3                  amd64        Python 3 bindings for the Cairo vector graphics library
ii  python3-cairo-dev                             1.10.0+dfsg-5                     all          Python 3 cairo bindings: development files
ii  python3-cairocffi                             0.7.2-2.1                         all          cffi-based cairo bindings for Python (Python3)
ii  python3-cairosvg                              1.0.20-1                          all          SVG to PDF/PS/PNG converter based on Cairo (Python3 library)
ii  python3-gi-cairo                              3.22.0-2+b1                       amd64        Python 3 Cairo bindings for the GObject library
hal-314 commented 6 years ago

I run with similar issue. I wanted to render a svg file inside wxPython. I used librsvg binding for python. It worked fine until I import wx.lib.wxcairo. Then, I ran with the same issue of @manns : KeyError: 'could not find foreign type Context'

I have wxPython 4.0.0b2 installed from pip

Sample code (uncomment the import to get the error):

import math
# import wx.lib.wxcairo # uncomment and error "KeyError: 'could not find foreign type Context'" will be raised  
import cairo
import gi
gi.require_version("Rsvg", "2.0")
from gi.repository import Rsvg

# Cairo basic painting works as expected
WIDTH, HEIGHT = 256, 256
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)

ctx.scale (WIDTH, HEIGHT) # Normalizing the canvas

pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0)
pat.add_color_stop_rgba (1, 0.7, 0, 0, 0.5) # First stop, 50% opacity
pat.add_color_stop_rgba (0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity

ctx.rectangle (0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
ctx.set_source (pat)
ctx.fill ()

ctx.translate (0.1, 0.1) # Changing the current transformation matrix

ctx.move_to (0, 0)
# Arc(cx, cy, radius, start_angle, stop_angle)
ctx.arc (0.2, 0.1, 0.1, -math.pi/2, 0)
ctx.line_to (0.5, 0.1) # Line to (x,y)
# Curve(x1, y1, x2, y2, x3, y3)
ctx.curve_to (0.5, 0.2, 0.5, 0.4, 0.2, 0.8)
ctx.close_path ()

ctx.set_source_rgb (0.3, 0.2, 0.5) # Solid color
ctx.set_line_width (0.02)
ctx.stroke ()
surface.write_to_png ("example.png")

# Error is raised if wx.lib.wxcairo is imported
rsvg = Rsvg.Handle.new_from_file('svg-image.svg')
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500)
ctx = cairo.Context(s)
rsvg.render_cairo(ctx) # the error is raised in this line

In Ubuntu 16.04, in addition of cairo packages need for wxPython, you need gir1.2-rsvg-2.0: 'sudo apt-get install gir1.2-rsvg-2.0'

fritz-k commented 5 years ago

Could this be an issue with python3-cairocffi being installed in @manns's environment and not in @swt2c's? If cairocffi is installed wx.lib.wxcairo.ContextFromDC(mdc) returns a cairocffi.context.Context object, otherwise a cairo.Context one. The PyGObject docs state that cairocffi isn't supported.

swt2c commented 5 years ago

@fritz-k you hit the nail on the head! I installed python3-cairocffi and now I see the same error.

I am not sure how to resolve this, though. Uninstalling cairocffi might be a workaround, but it might not be possible in @manns case. It seems like it might be better if pycairo was the default, and perhaps users could opt into using cairocffi somehow. @RobinD42 thoughts?