Daniyal
Daniyal

Reputation: 41

How to port Python program from gtk2 to gtk3?

We are trying to port this program from gtk2 to gtk3. When I try pygi-convert.sh I got the following error:

Traceback (most recent call last):
  File "scripts/amir", line 53, in <module>
    from gi.repository import GObject
  File "/usr/lib/python2.7/dist-packages/gi/__init__.py", line 39, in <module>
    raise ImportError(_static_binding_error)
ImportError: When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject". See: https://bugzilla.gnome.org/show_bug.cgi?id=709183

Any suggestion or other way to do this port?

Upvotes: 3

Views: 2181

Answers (1)

liberforce
liberforce

Reputation: 11454

First, try to understand what you're doing. Read the basics of official Python for GTK+ 3 tutorial. You'll see that the correct order to do imports is:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

The pygi-convert.sh is no silver bullet, you have to understand the changes and review them. For example the imports in this section are most certainly wrong. The order is not correct, and you still have an import gtk left on the import gtk,Gtk.glade line. A git grep -w gtk should help you find all the lowercase gtk occurences in your code, which should be removed or replaced.

I also see you have in that same file some code to change the window theme. Theming changed completely between GTK+ 2 and GTK+ 3. GTK+ 3 uses a CSS engine.

Check out the Python GTK+ 3 API to know what's available, and check in the C migration guide (not aware of a python resource for that, sorry) what has changed between GTK+ 2 and GTK+ 3. Most things won't apply to python, but some will. For example migrating from the old expose-event to the draw signal when doing custom drawing, or checking which widgets have been killed and replaced.

Then try to run, fix errors, rinse, repeat.

Upvotes: 5

Related Questions