using python as a GUI interface for C++ code.

Started by
3 comments, last by SiCrane 15 years, 6 months ago
Hi all. Newbie question. I have piece of a long C++ algorithm. i would like to create a GUI for it using Python. I've played around a bit with boost::python, and it seems to work nicely. My problem is this: I can call the wrapped C++ functions with ease from the python script, however, sometimes i need the C++ code to interact with the GUI. For example i would like the C++ code to be able to update an image in the GUI or to print some text. I do not know how to do so. I guess i could break stuff to smaller functions and let python call each of them and use their returned values to update the interface, however, i would like to make it more of an event driven design. In addition, in my particular case, the GUI is for a debug purposes. Making the C++ code to interact with the GUI, will save me lots of development time. is there an elegant method to accomplish that? thanks all.
Advertisement
You can pass Python objects to C++ as boost::python::object's. From there you can use attr() to get access to individual members of the object such as methods. If you want a specific example, try posting something that you're trying to do.
Quote:Original post by SiCrane
You can pass Python objects to C++ as boost::python::object's. From there you can use attr() to get access to individual members of the object such as methods. If you want a specific example, try posting something that you're trying to do.


thanks SiCrane.
is that the common design approach for such task?
if not, what is the proper design approach i should go for?
(by "task", i mean using another higher level interpreted language for a UI)

are there any documentation or code examples for what I'm trying to do i can look at?

please feel free to tell me I'm going totally in the wrong direction. I'm mainly guessing here about how i should approach the problem. :)

I dont have the exact C++ code yet, but what i had in mind, is some image processing algorithm, with various steps. I would like that each step will display the current temporary image in the UI for debug purposes. So, i can see in the UI, all the temp images of the different stages together on screen.
Quote:Original post by amitm02
are there any documentation or code examples for what I'm trying to do i can look at?


You could take a look at a little app I wrote to generate normal maps from heightmaps. The GUI is done in wxPython, the image processing in c++, hooked together with boost.python as you describe. I'm not sure it's the 'best' way to do things, but it works.

app
code

I ended up keeping all the image data in c++ and displaying images with opengl - it seemed easier than passing the image data to python. A strategy I considered for doing this was just to pass it as a string (e.g. raw bytes in a string object) - PIL can work with images in this format.

Again, not sure what I'm showing you is 'best', but I'm sure SiCrane will steer you in the right direction anyway [smile]

[size="1"]
There really is no "best" in the case of talking about binding arbitrary high level and low level languages. Passing a python object to C++ is probably what I would do in the specific case of C++/Python, but if I was using a C# GUI with C on Windows, I might just pass a HWND from C# and use SendMessage() in C.

This topic is closed to new replies.

Advertisement