<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Hola a todos, finalmente despues de leerme varias paginas de internet sobre el tema, he conseguido parcialmente comunicar una aplicacion python GUI con un programa en C.<br>Pero lamentablemente tengo algunos problemas que no he podido solucionar.<br><br>Para compilar el programa uso: gcc ccalc.c -g -fPIC -I/usr/include/python2.6 -shared -L/usr/bin -lpython2.6 -o ccalc.so<br>Para ejecutar el programa lanzo: python calc_fx.py<br><br>Adjunto codigos fuentes de archivos.<br><br>Este archivo queda intacto, no se modifica.<br>calc.py<br>*********<br># -*- coding: utf-8 -*-<br><br># Form implementation generated from reading ui file 'calc.ui'<br>#<br># Created: Wed Oct 14 02:06:16 2009<br>#      by: PyQt4 UI code generator 4.4.4<br>#<br># WARNING! All changes made in this file will be lost!<br><br>from PyQt4 import QtCore, QtGui<br><br>class Ui_calc(object):<br>    def setupUi(self, calc):<br>        calc.setObjectName("calc")<br>        calc.resize(400, 180)<br>        self.label = QtGui.QLabel(calc)<br>        self.label.setGeometry(QtCore.QRect(20, 13, 80, 31))<br>        self.label.setObjectName("label")<br>        self.label_2 = QtGui.QLabel(calc)<br>        self.label_2.setGeometry(QtCore.QRect(20, 60, 80, 18))<br>        self.label_2.setObjectName("label_2")<br>        self.label_3 = QtGui.QLabel(calc)<br>        self.label_3.setGeometry(QtCore.QRect(20, 100, 70, 18))<br>        self.label_3.setObjectName("label_3")<br>        self.num1 = QtGui.QLineEdit(calc)<br>        self.num1.setGeometry(QtCore.QRect(100, 14, 113, 30))<br>        self.num1.setObjectName("num1")<br>        self.num2 = QtGui.QLineEdit(calc)<br>        self.num2.setGeometry(QtCore.QRect(100, 55, 113, 28))<br>        self.num2.setObjectName("num2")<br>        self.result = QtGui.QLineEdit(calc)<br>        self.result.setGeometry(QtCore.QRect(100, 96, 113, 28))<br>        self.result.setObjectName("result")<br>        self.sum = QtGui.QPushButton(calc)<br>        self.sum.setGeometry(QtCore.QRect(240, 10, 106, 27))<br>        self.sum.setObjectName("sum")<br>        self.res = QtGui.QPushButton(calc)<br>        self.res.setGeometry(QtCore.QRect(240, 40, 106, 27))<br>        self.res.setObjectName("res")<br>        self.mul = QtGui.QPushButton(calc)<br>        self.mul.setGeometry(QtCore.QRect(240, 70, 106, 27))<br>        self.mul.setObjectName("mul")<br>        self.div = QtGui.QPushButton(calc)<br>        self.div.setGeometry(QtCore.QRect(240, 100, 106, 27))<br>        self.div.setObjectName("div")<br>        self.salir = QtGui.QPushButton(calc)<br>        self.salir.setGeometry(QtCore.QRect(130, 140, 106, 27))<br>        self.salir.setObjectName("salir")<br><br>        self.retranslateUi(calc)<br>        QtCore.QObject.connect(self.salir, QtCore.SIGNAL("clicked()"), calc.close)<br>        QtCore.QMetaObject.connectSlotsByName(calc)<br><br>    def retranslateUi(self, calc):<br>        calc.setWindowTitle(QtGui.QApplication.translate("calc", "Calculadora", None, QtGui.QApplication.UnicodeUTF8))<br>        self.label.setText(QtGui.QApplication.translate("calc", "Número 1", None, QtGui.QApplication.UnicodeUTF8))<br>        self.label_2.setText(QtGui.QApplication.translate("calc", "Número 2", None, QtGui.QApplication.UnicodeUTF8))<br>        self.label_3.setText(QtGui.QApplication.translate("calc", "Resultado", None, QtGui.QApplication.UnicodeUTF8))<br>        self.sum.setText(QtGui.QApplication.translate("calc", "Sumar", None, QtGui.QApplication.UnicodeUTF8))<br>        self.res.setText(QtGui.QApplication.translate("calc", "Restar", None, QtGui.QApplication.UnicodeUTF8))<br>        self.mul.setText(QtGui.QApplication.translate("calc", "Multiplicar", None, QtGui.QApplication.UnicodeUTF8))<br>        self.div.setText(QtGui.QApplication.translate("calc", "Dividir", None, QtGui.QApplication.UnicodeUTF8))<br>        self.salir.setText(QtGui.QApplication.translate("calc", "Salir", None, QtGui.QApplication.UnicodeUTF8))<br><br>********* <br><br>Archivo python con las funciones, problemas que tengo:<br>1.- valor recibido desde C es: <class '__main__.calc'><br>si se imprime el valor como texto (por eso he usado "%s" % (cresult) ) es: <__main__.calc object at 0x7f7aee08f848><br>el codigo 0x7f... siempre cambia en cada ejecucion.<br>2.- En la funcion ccalc.triggerEvent(5, 2), le he pasado los valores iniciales para comprobar el funcionamiento del archivo C.<br>Pero necesito que tome los valores que tienen self.num1 = QtGui.QLineEdit(calc) y self.num2 = QtGui.QLineEdit(calc) del archivo calc.py<br>he tratado con calc.num1.setText(), setupUi.num1.setText(), Ui_calc.num1.setText() pero no es reconocido.<br><br>calc_fx.py<br>*********<br># -*- coding: utf-8 -*-<br><br>import sys<br>from PyQt4.QtCore import SIGNAL<br>from PyQt4.QtGui import *<br>from calc import Ui_calc<br><br>class calc(QWidget, Ui_calc):<br>  def __init__(self, parent = None):<br>    QWidget.__init__(self, parent)<br>    self.setupUi(self)<br>    self.connect(self.sum, SIGNAL("clicked()"),self.sumar)<br>    self.connect(self.res, SIGNAL("clicked()"),self.restar)<br>    self.connect(self.mul, SIGNAL("clicked()"),self.multiplicar)<br>    self.connect(self.div, SIGNAL("clicked()"),self.dividir)<br><br>  def sumar(cresult):<br>    return cresult.result.setText("%s" % (cresult))<br>  <br>  def restar(cresult):<br>    return cresult.result.setText('')<br><br># Python calls a C extension module to register handlers, trigger events<br>import ccalc<br>ccalc.setHandler(calc.sumar)<br>ccalc.triggerEvent(5, 2)<br><br>if __name__ == "__main__":<br>  app = QApplication(sys.argv)<br>  window = calc()<br>  window.show()<br>  sys.exit(app.exec_())<br><br>*********<br><br><br>Archivo C, dudas que tengo:<br>El archivo al parecer esta trabajando bien a excepcion del valor que devuelve a python.<br>Si se agrega un printf bajo las lineas:<br>args = Py_BuildValue("d", cresult);<br>pyresult = PyEval_CallObject(Handler, args);<br>se aprecia que el valor de args y pyresult son correctos.<br>El problema esta en Py_BuildValue("d", cresult); al parecer no esta pasando el valor en forma adecuada.<br><br>ccalc.c<br>*********<br>#include <Python.h><br>#include <stdlib.h><br><br>static PyObject *Handler = NULL;     // keep Python object in C<br><br>void Route_Event(double cresult) {<br>    double *cres;<br>    PyObject *args, *pyresult;<br>    <br>    args = Py_BuildValue("d", cresult);   /* make arg-list */<br>    pyresult = PyEval_CallObject(Handler, args);      /* apply: run a call */<br>    Py_DECREF(args);                              /* add error checks */<br><br>    if (pyresult != NULL) {<br>        PyArg_Parse(pyresult, "f", &cres);<br>        printf("%f\n", cres);<br>        Py_DECREF(pyresult);<br>    }<br>}<br><br>static PyObject *<br>Register_Handler(PyObject *self, PyObject *args) {<br>    /* save Python callable object */<br>    Py_XDECREF(Handler);                 /* called before? */<br>    PyArg_Parse(args, "O", &Handler);    /* one argument? */<br>    Py_XINCREF(Handler);                 /* add a reference */<br>    Py_INCREF(Py_None);                  /* return 'None': success */<br>    return Py_None;<br>}<br><br>// let Python simulate event caught by C<br>static PyObject *<br>Trigger_Event(PyObject *self, PyObject *args) {<br>    double num1 = 0, num2 = 0;<br>    double cresult;<br>    PyArg_ParseTuple(args,"dd", &num1, &num2);    // carga variables python en punteros de C.<br>    cresult = num1 + num2;<br>    Route_Event(cresult);<br>    Py_INCREF(Py_None);<br>    return Py_None;<br>}<br><br>static struct PyMethodDef ccalc_methods[] = {<br>    {"setHandler", Register_Handler},<br>    //{"triggerEvent", Trigger_Event},<br>    {"triggerEvent",  Trigger_Event, METH_VARARGS},<br>    {NULL, NULL}<br>};<br><br>// this is called by Python on first "import ccalc"<br>void initccalc() {<br>    (void) Py_InitModule("ccalc", ccalc_methods);<br>}<br><br>*********<br><br>Saludos y muchas gracias por vuestra ayuda.<br><br>                                         </body>
</html>