GUI under embedded Linux / python 3.3.2

时间:2015-09-01 23:00:15

标签: linux user-interface python-3.x

I'm writing a GTK+ app using embedded/extended Python3 as a scripting language. What I'd like do is make it possible for those scripts to include menus, but, so far, things aren't cooperating.

I do the usual python initialisation with:

PyImport_AppendInittab("gfig", &PyInit_gfig);
Py_SetProgramName (L"gfig");
Py_Initialize();

and then some setup with:

PyRun_SimpleString (
                 "import gfig\n"
                 "import sys\n" ........ )

(gfig is the app I'm writing, as well as the name of extension module.)

Then I run the app using

PyRun_File (fp, pyfile, Py_file_input, global_dict, local_dict);

to run the script:

from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")

        self.button = Gtk.Button(label="Click Here")
        self.button.connect("clicked", self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        print("Hello World")

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

This results in:

[0] /mnt/disk5/personal/tinkering/gf3/src >./gf py4

Traceback (most recent call last): File "py4", line 3, in

from gi.repository import Gtk File "",

line 1567, in _find_and_load File "",

line 1534, in _find_and_load_unlocked File "/usr/lib64/python3.3/site-packages/gi/importer.py",

line 68, inload_module dynamic_module._load() File "/usr/lib64/python3.3/site-packages/gi/module.py",

line 289, in _load self._overrides_module = importlib.import_module('gi.overrides.' + self._namespace) File "/usr/lib64/python3.3/importlib/init.py",

line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/lib64/python3.3/site-packages/gi/overrides/Gtk.py",

line 1523,in initialized, argv = Gtk.init_check(sys.argv)
AttributeError 'module' object has no attribute 'argv'

Here's the script:

from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")

        self.button = Gtk.Button(label="Click Here")
        self.button.connect("clicked", self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        print("Hello World")

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

which works fine when from the command line:

[0] /mnt/disk5/personal/tinkering/gf3/src >python3 py4

I see that the script ends with "Gtk.main()" which, since I'm running python embedded under GTK+, looks like it may give me trouble in the future.

(I've tried Tk, wx, and, I think, Qt. None of them worked, but I didn't explore why not in any detail. I'd like to use Gtk.)

Any help greatly appreciated.

0 个答案:

没有答案