Is WxPython appropriate for a machine control GUI?

Most of what I do is embedded control code in C++, and mostly for Windows, but it’s becoming more frequent for customers to want Linux.The apps tend to be 2/3 control logic and about 1/3 user interface. But writing the user interface in C++ is not much fun and in general is not portable. At first, I considered doing the GUI in C#, but connecting C# to complex C++ is a mess and another portability problem. Then I ran across an article on WxPython.

My control logic is in the form of a C++ singleton class. At instantiation (by the GUI), it reads configuration data, creates a hierarchical state machine, and starts multiple worker threads. So most of the time-critical stuff is disconnected from the GUI (as long as the GUI doesn’t hog the CPU). The GUI is typically pretty simple, polling the control logic every 100 or 200 millisecs with start/stop/status type requests and displaying the results. The GUI also supplies an asynchronous callback that facilitates display of error screens, etc, although this could also be done by polling.

So after that long-winded-but-short-on-details explanation of the application, my questions are:

  1. does WxPython seem like a good fit for the user interface portion? i.e easy linking to C++ DLL.
  2. Could I expect decent performance for a “simple” GUI on a low end PC? I know this is subjective, but say compared to C#?
  3. Assuming that I get the C++ to port cleanly to Linux, could I expect an easy port of the Python?
  4. Best “get started” book for moving from C++ to Python?
    Thanks in advance!
  1. While it is possible to use C++ from Python it is much easier to use C from Python.

    • Using C++ from Python requests the usage of a specific framework. That’s what wxPython does.
      However, I’ve already used a C++ library quite simply by creating a small C wrapper and using ctypes. But you loose the object model (from the caller point of view).
    • Using C from Python can be done either by compiling a small C Python library or directly with ctypes. The former is much more efficient but the latter is much simpler (pure Python).
  2. wxPython is essentially a C++ library with a Python interface. I don’t know C# but I believe a wxPython app is less heavy than a Qt one.

  3. It will depend on which part of the API you use. There are widgets that work the same way on all OS and others that behave a little bit differently. Globally, it should not be a problem.

  4. Forget C++, learn Python :wink:

What you want to achieve is not an easy task. You’ll have to learn Python (if not already done) and the way to interface Python with C (quite easy) or C++ (much more difficult) and the GUI framework.

Other solutions :

  • Convert your C++ code to Python.
  • Create a complete C++ app. Create a Python GUI app. Use IPC (socket ?) to communicate between both. This might have the advantage that to GUI can be run on a machine and the C++ app on another one.
  • Use wxWidgets.
  • Use Wt.

I use wxPython to bind C++ code quite frequently, and up until this year I have been wrapping the C++ in a C-DLL (ugh!) then using Python ctypes to bind the C to Python (double-ugh!). Then I discovered a project called pybind11, and I will never go back. It supplants what boost.python was designed to do, with direct support for many C++11 types.
For example, I have a set of three C++ classes wrapping PCIe driver code (driver APIs from two vendors, plus a simulator I wrote). In pybind, I was able to duplicate exactly same the class heirarchy without rewriting any of it, using built-in macros to set up base-class default functions, base class virtual functions, and subclass overrides! The whole project builds using the C++ compiler, into a .pyc (or .pyd, I forget) file. Which you can then import as a module into your wxPython project and wrap nicely in a wx GUI.
So, in short:

  1. yes.
  2. yes. C# and Python have a lot in common with regard to using a runtime and just-in-time bytecode, so I’d expect similar performance.
  3. yes.
    4.“wxPython in Action” for the GUI stuff, “Learning Python” (OReilly) and lots of others for garden variety Python.
    Post back if you need specific pointers, this is a lot to pick up at once.
    Good luck,
    Tanaya

Agree completely with Tanaya’s comments.
Some extra points:

  1. I use wxPython primarily on Windows, but also on Mac. All the GUI code (wxPython) worked
    first time, without modifications, porting to Mac.
  2. Wt has onerous licensing - wxPython is unencumbered.
  3. I also connect to embedded code, etc., and have not found any problems with overhead of running Python.
  4. For distributing a hybrid Python/wxPython/C++ application, try pyinstaller. Pyinstaller produces a single .exe file that you can then send to clients etc.
  5. wxPython makes GUI development a lot less painful than C++ (eg. MFC). Go for it!

Adrian

@NicoPi,
I can’t easily convert the control code to Python, as I use quite a few vendor Dlls to connect to servo drives, I/O, sensors, etc.

Separating the control and the GUI completely with IPC between them is an option I’ve looked into and is possible, but carries quite a bit of overhead for such little functionality.

wxWidgets is an interesting possibility - I’ll look deeper into this.

Thanks for your input - I have some options to consider!

@Tanaya,
Thanks for your input. I will check out pybind11 and the books you suggested.

@Adrian,
This is also very good input!

A few paragraphs from experienced developers is more helpful than days of Google searches!

That’s very interesting. I’ll have a look at it. Thanks.

The license we use at work is not very expensive. But it depends on what you call expensive :wink: .
If I remember well, there is a dual license model. A commercial license and an open source one. The license you need depends on your product.

Wt is useful to create app GUI that can be used (with a web browser) on many devices (iOS, Android, desktop linux, destop Windows…) without creating an app for each OS.
It is also useful for any app remotely controllable with a web browser…
Wt has a Qt like API and instantiate a web server to serve the GUI (the network throughput is not light).
I will not recommend Wt except for the 2 above cases.

Nicolas

@NicoPi,
Thanks for the additional info. Most customers in my world hate licenses due to cost and liability, and most budgets call for lower end hardware, which in turn require generally lightweight coding frameworks.

Here’s an example of using wxPyton for a machine control GUI:

I don’t recall many details but about a decade ago wxPython was used for the graphical control interface on the machine that was used to drill kilometer-deep shafts in the ice at the South Pole for the IceCube Neutrino Observatory. IIRC it was also used for front-ends of some of the monitoring and maintenance equipment as well.

This means that wxPython applications have been deployed on all seven continents! :smile: