Can C programs have a Python GUI?

My friend has an application written in C that comes with a graphical interface created using GTK under Linux. Now we want to rewrite the GUI in python ( wxpython or PyQT ).

I have no experience with Python and I don’t know how to get Python to interact with C. I would like to know if this is possible, and if so, how do I implement it?

+6
source share
3 answers

Yes, it can be called 'C' from Python.

Please check out SWIG (deprecated), Python also provides its own extensibility API. You can look at it.

Also google CTypes.

LINKS:

Python extension

Simple example: I used Cygwin for Windows for this. My version of python on this computer - 2.6.8 - was tested with test.py, loading a module called "myext.dll" - it works fine. You might want to modify the Makefile so that it works on your computer.

original.h

 #ifndef _ORIGINAL_H_ #define _ORIGINAL_H_ int _original_print(const char *data); #endif /*_ORIGINAL_H_*/ 

original.c

 #include <stdio.h> #include "original.h" int _original_print(const char *data) { return printf("o: %s",data); } 

stub.c

 #include <Python.h> #include "original.h" static PyObject *myext_print(PyObject *, PyObject *); static PyMethodDef Methods[] = { {"printx", myext_print, METH_VARARGS,"Print"}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initmyext(void) { PyObject *m; m = Py_InitModule("myext",Methods); } static PyObject *myext_print(PyObject *self, PyObject *args) { const char *data; int no_chars_printed; if(!PyArg_ParseTuple(args, "s", &data)){ return NULL; } no_chars_printed = _original_print(data); return Py_BuildValue("i",no_chars_printed); } 

Makefile

 PYTHON_INCLUDE = -I/usr/include/python2.6 PYTHON_LIB = -lpython2.6 USER_LIBRARY = -L/usr/lib GCC = gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/include -I/usr/include/python2.6 win32 : myext.o - gcc -shared myext.o $(USER_LIBRARY) $(PYTHON_LIB) -o myext.dll linux : myext.o - gcc -shared myext.o $(USER_LIBRARY) $(PYTHON_LIB) -o myext.so myext.o: stub.o original.o - ld -r stub.o original.o -o myext.o stub.o: stub.c - $(GCC) -c stub.c -o stub.o original.o: original.c - $(GCC) -c original.c -o original.o clean: myext.o - rm stub.o original.o stub.c~ original.c~ Makefile~ 

test.py

 import myext myext.printx('hello world') 

OUTPUT

o: hello world

+5
source

Sorry, but I don't have python experience, so I don’t know how to get Python to interact with C.

Yes, that’s how you do it. Turn your C code into a Python module, and then you can write the entire GUI in Python. See Extending and Embedding a Python Interpreter .

+2
source

If you have an option between C or C # (Sharp), go to C # and use visual studio, you can create a graphical interface by easily dragging and dropping components. If you want to do something in python, find wxPython . Java has a built-in graphical interface known as swing. You will need some tutorials, but if this program should not be portable, just go with C # and create it in 10 minutes.

Alternatively, you can write your C code and export it as a python module, which you can download from python. It's not very difficult to configure some C functions and use the python GUI that calls them. For this you can use SWIG, Pyrex, BOOST .

0
source

All Articles