Python Forum

Full Version: Interacting with python console while program is running
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I would like to write a program with a gui, which is able to interact with a python console. I would like to draw something in the gui, send the values to the console and there I can calculate something and plot the data. Do I have to take two consoles to run something like this, or is it possible to execute the gui in a second thread and I'm able to use the python console to plot or calculate something with the data I got from the gui?
Attached is a sketch, which shows what I would like to do.
At the end it should be possible to create something like simulink with matlab.

[Image: spyder.png?part=0.1&view=1&vt=ANaJVrH9wg...uwZPhqiCyE]

Best regards
One way to do this could be to incorporate a python console in your GUI application. I have an example of how this can be done in pygtk. I'm not the author of this program (see the credits at the top of the file), it was part of the Umit project but as you see it was free software under GPLv2 when I downloaded it a long time ago. It still runs under python 2.7 and it is basically both a pygtk widget and a python console.

This program could be extended by adding widgets to draw anything you want and the console could access directly the drawing data.

The program is not very long, so I think it can be rewritten for other gui toolkits and versions of python without too much effort. The file is here: umitconsole.py
I visited the pygtk website and downloaded the most current version, which is for Python 2.7 and was released on 2012-02-09. There appears to be no version for Python 3.x and since nothing has been changed for 6 years this module is no longer actively developed. The msi installer for Windows is 32 megabytes, so you'll be adding 32 MB to your distribution if you publish just to include it.
You're probably better off using the Text widget in tkinter since what you're proposing would only require strict control over keyboard and mouse input in it through event handlers, use of exec() and eval() to process lines, and then capturing their output by redirecting the standard out and standard error streams to capture them and display them in the Text widget after the line of code is entered. If you can do this, you have complete control to add whatever features you your standard console implementation.