2011-11-13 17 views
12

我有麻煩理解gi.repository瞭解gi.repository

我用這個敷設渠道在我的代碼

from gi.repository import Gtk 

但是,如果我想使用一些組件

我搜索我得到導入錯誤我得到它的一些組件,如GtkSource,Vte,GLib,...

所以我的代碼就像

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

一切工作正常,但如果我想添加matplotlib吸取我的畫布,我得到和錯誤

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 

我怎樣才能得到matplotlib與gi.repository工作?

謝謝

回答

6

這是一個非常好的問題。恐怕答案可能是「你不能。」 Matplotlib的GTK後端是爲PyGTK編寫的,這是GTK的老式Python綁定。 gi.repository包是新式的Python綁定。我不知道他們是否可以混合,但你的結果似乎表明他們不能。

+2

11個月前一個GTK3後端與新的gi.repository導入合併在github中:https://github.com/matplotlib/matplotlib/pull/590我不確定什麼版本有這個或如何調用它,但它*應該*兼容...不幸的是,正如我最近發現的,你不能在同一個應用程序中同時使用兩個導入,它會崩潰(分段錯誤)。 – NoBugs

13

看來對Gtk3的支持是added recently。我想這需要一些時間,直到它在主要發行版中可用。

最好的解決方案是下載並安裝最新版本。

作爲一種變通方法,以避免在我的Ubuntu 11.10安裝的東西,我有dowloaded backend_gtk3.py and backend_gtk3agg.py files和直接導入,如:

from gi.repository import Gtk 

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

我不得不改變backend_gtk3agg.py線6,它說:

import backend_agg 

from matplotlib.backends import backend_agg 

,所以它可以從我的installatio導入模塊ñ。 到目前爲止,它適用於我,但我明白這個解決方案無法與不同版本的matplotlib一起工作。

+0

爲此而歡呼 - 做得很好:-) – Coops

+0

嗨,你是說你只是下載了兩個文件?我正在嘗試這個例子:http://matplotlib.org/dev/examples/user_interfaces/embedding_in_gtk3.html使用你解釋的過程,但它不起作用。你能否詳細說明一下。你是否也使用本地matplotlib安裝的圖形? – Fabrizio