Question on Embedding Python GUI call from C code.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bappai
    New Member
    • Jun 2008
    • 2

    Question on Embedding Python GUI call from C code.

    Hello,

    I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also.

    I am faced with a peculiar problem. I actually tried out an embedding code (downloaded from the net) which actually calls any function which has a text display but when I try to call any code which opens cleanly in python prompt it crashes.

    To make matters more concrete let me paste the code of C++ to Python call.

    #include <Python.h>

    int main(int argc, char *argv[])
    {
    PyObject *pName, *pModule, *pDict, *pFunc, *pValue;

    if (argc < 3)
    {
    printf("Usage: exe_name python_source function_name\n ");
    return 1;
    }

    // Initialize the Python Interpreter
    Py_Initialize() ;

    PyRun_SimpleStr ing("import sys");
    PyRun_SimpleStr ing("sys.path.a ppend(\"./ui\")");
    // Build the name object
    pName = PyString_FromSt ring(argv[1]);

    // Load the module object
    pModule = PyImport_Import (pName);

    // pDict is a borrowed reference
    pDict = PyModule_GetDic t(pModule);

    // pFunc is also a borrowed reference
    pFunc = PyDict_GetItemS tring(pDict, argv[2]);

    if (PyCallable_Che ck(pFunc))
    {
    PyObject_CallOb ject(pFunc, NULL);
    } else
    {
    PyErr_Print();
    }

    // Clean up
    Py_DECREF(pModu le);
    Py_DECREF(pName );

    // Finish the Python Interpreter
    Py_Finalize();

    return 0;
    }


    Now my understanding is that when this code is run, for any python module it should be able to call the function. I actually tried out the multiply function available and it gives correct results.

    But if I do <program> Tkinter _test() , instead of a Tk window popping up the program crashes.

    I would be very thankful if anyone can suggest where can I go wrong in this since I am a total newbie in GUI and Python.

    Regards
    Sumanta
  • kaarthikeyapreyan
    New Member
    • Apr 2007
    • 106

    #2
    I am faced with a peculiar problem. I actually tried out an embedding code (downloaded from the net) which actually calls any function which has a text display but when I try to call any code which opens cleanly in python prompt it crashes.
    What is your actual error ?
    where does your program crash ? is the error during your c code compilation or during the execution of the c code after your executable is created ,and could you brief me more on what is inside this.
    Code:
    PyRun_SimpleString("sys.path.append(\"./ui\")");

    Comment

    • bappai
      New Member
      • Jun 2008
      • 2

      #3
      Originally posted by kaarthikeyaprey an
      What is your actual error ?
      where does your program crash ? is the error during your c code compilation or during the execution of the c code after your executable is created ,and could you brief me more on what is inside this.
      Code:
      PyRun_SimpleString("sys.path.append(\"./ui\")");

      Hello,

      First of all I apologize for the late reply. I am totally new to the forum and so did not have an idea that a mail did not go to the userid if somebody replied.

      Anyway, to answer the questions

      1. the failure is never in the compilation part. I had downloaded cygwin (latest version) compiled Python 2.5.2 on that linked my program correctly and the problem arose during runtime when it crashed.

      2. For the question as to what is there in CODE]PyRun_SimpleStr ing("sys.path.a ppend(\"./ui\")");[/CODE][/QUOTE] , there is a small python script which I am pasting for your reference.

      #!/usr/bin/python
      # -*- coding: iso-8859-1 -*-

      import Tkinter

      class simpleapp_tk(Tk inter.Tk):
      def __init__(self,p arent):
      print "Second.... .\n"
      Tkinter.Tk.__in it__(self,paren t)
      print "Thirde.... .\n"
      self.parent = parent
      #self.initializ e()

      def hello():
      print "hello!"
      def initialize(self ):
      self.grid()

      menubar = Tkinter.Menu(se lf)
      #menubar.add_co mmand(label="Su manta Test", command=self.he llo)
      # create a pulldown menu, and add it to the menu bar
      filemenu = Tkinter.Menu(me nubar, tearoff=0)
      filemenu.add_co mmand(label="Op en", command=self.he llo)
      filemenu.add_co mmand(label="Sa ve", command=self.he llo)
      filemenu.add_se parator()
      filemenu.add_co mmand(label="Ex it", command=self.qu it)
      menubar.add_cas cade(label="Fil e", menu=filemenu)

      # create more pulldown menus
      editmenu = Tkinter.Menu(me nubar, tearoff=0)
      editmenu.add_co mmand(label="Cu t", command=self.he llo)
      editmenu.add_co mmand(label="Co py", command=self.he llo)
      editmenu.add_co mmand(label="Pa ste", command=self.he llo)
      menubar.add_cas cade(label="Edi t", menu=editmenu)

      helpmenu = Tkinter.Menu(me nubar, tearoff=0)
      helpmenu.add_co mmand(label="Ab out", command=self.he llo)
      menubar.add_cas cade(label="Hel p", menu=helpmenu)

      # display the menu
      self.config(men u=menubar)

      #self.entryVari able = Tkinter.StringV ar()
      #self.entry = Tkinter.Entry(s elf,textvariabl e=self.entryVar iable)
      #self.entry.gri d(column=0,row= 0,sticky='EW')
      #self.entry.bin d("<Return>", self.OnPressEnt er)
      #self.entryVari able.set(u"Ente r text here.")

      #button = Tkinter.Button( self,text=u"Cli ck me !",
      # command=self.On ButtonClick)
      #button.grid(co lumn=1,row=0)

      #self.labelVari able = Tkinter.StringV ar()
      #label = Tkinter.Label(s elf,textvariabl e=self.labelVar iable,
      # anchor="w",fg=" white",bg="blue ")
      #label.grid(col umn=0,row=1,col umnspan=2,stick y='EW')
      #self.labelVari able.set(u"Hell o !")

      #self.grid_colu mnconfigure(0,w eight=1)
      self.resizable( True,False)
      self.update()
      self.geometry(s elf.geometry())

      self.canvas=Tki nter.Canvas(sel f, bg="white", width=400, height=400,
      bd=0, highlightthickn ess=0)
      self.canvas.pac k()

      #self.entry.foc us_set()
      #self.entry.sel ection_range(0, Tkinter.END)

      def OnButtonClick(s elf):
      self.labelVaria ble.set( self.entryVaria ble.get()+" (You clicked the button)" )
      self.entry.focu s_set()
      self.entry.sele ction_range(0, Tkinter.END)

      def OnPressEnter(se lf,event):
      self.labelVaria ble.set( self.entryVaria ble.get()+" (You pressed ENTER)" )
      self.entry.focu s_set()
      self.entry.sele ction_range(0, Tkinter.END)

      def simple_fn():
      print "Hello and Welcome"
      app = simpleapp_tk(No ne)

      # The problem comes when the simpleapp_tk call is made and inside
      #there when Tkinter is called.
      # app.title('my app')
      # app.mainloop()


      #if __name__ == "__main__":
      # app = simpleapp_tk(No ne)
      # app.title('my application')
      # app.mainloop()

      Comment

      Working...