Understanding gi.repository - python

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

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.

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.

Related

GTK module not found in Python (PyCharm)

How can I avoid the following error:
GTK module not found in Python (PyCharm), please see the snippet below.
This answer hasn't helped.
(Still cannot find gtk in pip.)
if you are using pyhon 3.0+ then import this library like this: from gi.repository import Gtk

GTK error when using opencv with python in ubuntu

I am trying to follow a tutorial found here to calibrate a camera via a chessboard image, using python and opencv in Ubuntu 18.04. Everything goes well until the program gets to the cv2.imshow() function, at which point I get the following error:
Gtk-ERROR **: 19:31:06.612: GTK+ 2.x symbols detected. Using GTK+ 2.x
and GTK+ 3 in the same process is not supported Trace/breakpoint trap
(core dumped)
I can only get around this error by commenting out the function. As I am quite new to programming, I don't know how to troubleshoot this further. Searching for this error online shows it being encountered in scenarios different from mine. I would really appreciate any help in resolving this. Please let me know if more information is needed.
The error message is telling you that you're trying to load two different major versions of GTK in the same process—either because you're importing the Gtk module like:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
or because the OpenCV Python module is trying to side-load a GTK module by itself. Since OpenCV seems to use Qt, it might be a case of a GTK module being loaded in your environment to match the system's style.

Gtk object has no attribute 'ICON_SIZE_BUTTON'

I'm nearly new to python and new to gtk.
I just can't figure out, why I get this error message:
self.builder.get_object("checkstatus").set_from_stock("gtk-yes", Gtk.ICON_SIZE_BUTTON)
.......
AttributeError: 'gi.repository.Gtk' object has no attribute 'ICON_SIZE_BUTTON'
At the beginning I'm importing:
import pygtk
pygtk.require('2.0')
from gi.repository import Gtk
So the problem is that gtk has no no attribute called 'ICON_SIZE_BUTTON'?
But when I look at the documentation it says so...
http://developer.gnome.org/pygtk/2.22/class-gtkimage.html#method-gtkimage--set-from-stock
I would appreciate any help. Thanks!
Well, there's a few things going on.
For GTK+ 3 which uses PyGObject, you use from gi.repository import Gtk and then use Gtk.IconSize.BUTTON.
For GTK+ 2 which uses PyGTK you use import pygtk and import gtk and then use gtk.ICON_SIZE_BUTTON.
In other words, you're mixing up versions. PyGTK (GTK 2) was replaced by PyGObject and something called "gobject introspection" in GTK 3. Check out this tutorial: http://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html

Is there a way to get code-hints for gtk3 and python working on aptana?

My situation is Aptana eclipse plug-in installed and working properly.
If I from gi.repository import Gtk the code completion I'm used to from import gtk is gone. Any solutions or is it just a bug?
My interim solution is keeping an import gtk\ngtk. in a clipboard manager and dropping it in when I need hints.
Yes, there is!
You should add gi to the forced builtins and you'll get a code completion:
Go to the Window->Preferences, PyDev->Interpreter python section and add gi to the "Forced builtin" tab for each interpreter you would like to use Gtk3.
More about this:
http://pydev.org/manual_101_interpreter.html
from gi.repository import Gtk
is not same like
import gtk
With the statement "
from gi.repository import Gtk
your are importing Gtk 3. With
import gtk
your are importing Gtk 2.
I would say that you code completion doesn't know Gtk 3.
maybe useful:
Tutorial for Gtk 3 http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html
http://live.gnome.org/PyGObject

Problems importing GDK

I am trying to import GDK to my program however I continue to get an error
No module named GDK
Do you know how I can fix this? Since it was working before I already tried import gtk.GDK
and import GDK.
I have installed PyGTK and PyGDK is part of of it pyGTK.
Try:
import gtk.gdk
(note: small letters)

Categories