By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Question on Embedding Python GUI call from C code.

P: 2
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_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\"./ui\")");
// Build the name object
pName = PyString_FromString(argv[1]);

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

// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);

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

if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}

// Clean up
Py_DECREF(pModule);
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
Jun 24 '08 #1
Share this Question
Share on Google+
2 Replies


kaarthikeyapreyan
100+
P: 107
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.
Expand|Select|Wrap|Line Numbers
  1. PyRun_SimpleString("sys.path.append(\"./ui\")");
Jun 25 '08 #2

P: 2
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.
Expand|Select|Wrap|Line Numbers
  1. 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_SimpleString("sys.path.append(\"./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(Tkinter.Tk):
def __init__(self,parent):
print "Second.....\n"
Tkinter.Tk.__init__(self,parent)
print "Thirde.....\n"
self.parent = parent
#self.initialize()

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

menubar = Tkinter.Menu(self)
#menubar.add_command(label="Sumanta Test", command=self.hello)
# create a pulldown menu, and add it to the menu bar
filemenu = Tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=self.hello)
filemenu.add_command(label="Save", command=self.hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.quit)
menubar.add_cascade(label="File", menu=filemenu)

# create more pulldown menus
editmenu = Tkinter.Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=self.hello)
editmenu.add_command(label="Copy", command=self.hello)
editmenu.add_command(label="Paste", command=self.hello)
menubar.add_cascade(label="Edit", menu=editmenu)

helpmenu = Tkinter.Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=self.hello)
menubar.add_cascade(label="Help", menu=helpmenu)

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

#self.entryVariable = Tkinter.StringVar()
#self.entry = Tkinter.Entry(self,textvariable=self.entryVariable )
#self.entry.grid(column=0,row=0,sticky='EW')
#self.entry.bind("<Return>", self.OnPressEnter)
#self.entryVariable.set(u"Enter text here.")

#button = Tkinter.Button(self,text=u"Click me !",
# command=self.OnButtonClick)
#button.grid(column=1,row=0)

#self.labelVariable = Tkinter.StringVar()
#label = Tkinter.Label(self,textvariable=self.labelVariable ,
# anchor="w",fg="white",bg="blue")
#label.grid(column=0,row=1,columnspan=2,sticky='EW ')
#self.labelVariable.set(u"Hello !")

#self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())

self.canvas=Tkinter.Canvas(self, bg="white", width=400, height=400,
bd=0, highlightthickness=0)
self.canvas.pack()

#self.entry.focus_set()
#self.entry.selection_range(0, Tkinter.END)

def OnButtonClick(self):
self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)

def OnPressEnter(self,event):
self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)

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

# 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(None)
# app.title('my application')
# app.mainloop()
Jun 27 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.