How to use Pygtk in a C/C++ application?
Question: I would like to integrate a simple Pygtk window in a C/C++ application (The
reason being a previously designed GUI in pyGtk is to be integrated into a
bigger GTK+ (in C) application)

I get Segmentation fault error

Here what i did :

=====> In python (tmp.py):

    
    
    #!/usr/bin/python
    
    #from gi.repository import Gtk, Gio
    
    win=Gtk.Window()
    win.connect("delete-event",Gtk.main_quit)
    win.show_all()
    Gtk.main()
    

======> In C/C++ (simple.cpp):

i want to just execute that little window

    
    
    #include <Python.h>
    
    int main(int argc, char *argv[])
    {
       Py_SetProgramName(argv[0]);  /* optional but recommended */
       Py_Initialize();
    
       FILE *fp      = fopen ("tmp.py",   "r+");
    
       PyObject *mainModule = PyImport_AddModule("__main__");
    
       PyObject * subModules = PyList_New(0);
       PyList_Append(subModules, PyString_FromString("Gtk"));
       PyList_Append(subModules, PyString_FromString("Gio"));
    
       PyObject *Gtkstuff = PyImport_ImportModuleEx("gi.repository",NULL,NULL,subModules);
    
       PyObject *GtkMod =PyObject_GetAttr(Gtkstuff,PyString_FromString("Gtk"));
       PyObject *GioMod =PyObject_GetAttr(Gtkstuff,PyString_FromString("Gio"));
    
       PyModule_AddObject(mainModule, "Gtk", GtkMod);
       PyModule_AddObject(mainModule, "Gio", GioMod);
    
       PyRun_SimpleFile(fp,"tmp.py");
       Py_Finalize();
       return 0;
    }
    

The way how i compile is :

    
    
    g++ $(python-config --cflags) -o simple $(python-config --ldflags) ./simple.cpp
    

The output :

    
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib64/python2.7/site-packages/gi/importer.py", line 68, in load_module
        dynamic_module._load()
      File "/usr/lib64/python2.7/site-packages/gi/module.py", line 291, in _load
        overrides_modules = __import__('gi.overrides', fromlist=[self._namespace])
      File "/usr/lib64/python2.7/site-packages/gi/overrides/Gtk.py", line 1502, in <module>
        initialized, argv = Gtk.init_check(sys.argv)
    AttributeError: 'module' object has no attribute 'argv'
    Traceback (most recent call last):
      File "tmp.py", line 5, in <module>
        win=Gtk.Window()
      File "/usr/lib64/python2.7/site-packages/gi/overrides/Gtk.py", line 415, in __init__
        if not initialized:
    NameError: global name 'initialized' is not defined
    


Answer: This works perfectly, the rest of the code doesn't make sense to me, since you
can do all the initialization in the python script.

    
    
    #include <Python.h>
    
    int main(int argc, char *argv[])
    {
        Py_SetProgramName(argv[0]); /* optional but recommended */
        Py_Initialize();
    
        FILE *fp = fopen("tmp.py", "r");
        if (fp == NULL)
            return 2;
    
        PyRun_SimpleFile(fp, "tmp.py");
        Py_Finalize();
    
        fclose(fp);
        return 0;
    }
    

