Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding gi.repository

I have troubles understanding gi.repository

I use this contruction in my code

from gi.repository import Gtk

But if I want to use some component I get import error

I searched and I got it worked for some components, like GtkSource, Vte, GLib, ...

So my code is like

from gi.repository import Gtk, GtkSource, Vte, GLib

Everything worked fine, but if I want to add matplotlib to draw on my canvas I get and error

    enter code/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py:40: Warning: specified class size for type `PyGtkGenericCellRenderer' is smaller than the parent type's `GtkCellRenderer' class size
  from gtk import _gtk
/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py:40: Warning: g_type_get_qdata: assertion `node != NULL' failed
  from gtk import _gtk
/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py:40: Warning: g_ascii_strncasecmp: assertion `s2 != NULL' failed
  from gtk import _gtk
Segmentation fault (core dumped) here

How can I get matplotlib working with gi.repository?

Thank you

like image 592
Jan Vorcak Avatar asked Nov 13 '11 05:11

Jan Vorcak


2 Answers

It seems that the support for Gtk3 it's been added recently. I guess it will take some time till it's available in the main distributions.

The best solution would be to download and install the latest version.

As a workaround to avoid installing stuff in my Ubuntu 11.10 I have dowloaded backend_gtk3.py and backend_gtk3agg.py files and imported directly like:

from gi.repository import Gtk

from matplotlib.figure import Figure
from backend_gtk3agg import FigureCanvasGTK3Agg as FigCanvas

I had to change backend_gtk3agg.py line 6 where it says:

import backend_agg

with

from matplotlib.backends import backend_agg

, so it can import the module from my installation. So far it works for me, but I understand this solution can't work with different versions of matplotlib.

like image 93
dlobato Avatar answered Oct 24 '22 01:10

dlobato


That's a very good question. I'm afraid the answer might be "you can't." Matplotlib's GTK backend is written for PyGTK, the old-style Python bindings for GTK. The gi.repository package is the new-style Python bindings. I don't know one way or the other whether they can mix or not, but your results seem to indicate they can't.

like image 33
ptomato Avatar answered Oct 24 '22 01:10

ptomato