Error “Could not find any typelib for Gtk” with Python3 and GTK3

不羁岁月 提交于 2019-11-28 05:30:48

问题


I cannot make Python3 work with GTK3. I'm in a cluster context and I had everything recompiled from the sources.

When I run a simple example :

from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

I have the following error :

    ERROR:root:Could not find any typelib for Gtk
    Traceback (most recent call last):
      File "gtk3_example.py", line 2, in 
        from gi.repository import Gtk
    ImportError: cannot import name 'Gtk'

回答1:


There are typically additional packages to install depending on what you want to introspect. The one I found that was crucial was gir1.2-gtk-3.0 (or 2.0 depending which version you are coding against).




回答2:


The problem is due to the compiled version of GTK3 that was not referenced in gobject-introspection.

It is simple to check the problem by listing the .typelib files in gobject-introspection


    $ which g-ir-scanner
    /Produits/publics/x86_64.Linux.RH6/gobject-introspection/1.40.0/bin/g-ir-scanner

    $ ls /Produits/publics/x86_64.Linux.RH6/gobject-introspection/1.40.0/lib/girepository-1.0/
    cairo-1.0.typelib     fontconfig-2.0.typelib  GIRepository-2.0.typelib  GModule-2.0.typelib  win32-1.0.typelib   xlib-2.0.typelib
    DBus-1.0.typelib      freetype2-2.0.typelib   GL-1.0.typelib            GObject-2.0.typelib  xfixes-4.0.typelib  xrandr-1.3.typelib
    DBusGLib-1.0.typelib  Gio-2.0.typelib         GLib-2.0.typelib          libxml2-2.0.typelib  xft-2.0.typelib

The problem should be solved by recompiling GTK (and its dependencies) with the configure option --enable-introspection=yes.




回答3:


Try using the following code:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GTK


来源:https://stackoverflow.com/questions/25037006/error-could-not-find-any-typelib-for-gtk-with-python3-and-gtk3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!