Skip to main content
gamedev.net
🔒 Locked

Python question, GTK or Glade

Started by Radiocreed May 6, 2005 at 5:58 PM 5 replies 1.6k views
Original Post
Radiocreed
Radiocreed
Let's say I have a C++ program like the following: int main() { for ( int i = 0; i < 10; i++ ) printf("%d", i); return 0; } Let's also assume that I have a python wrapper for the above called WrapMain. I want to invoke this C++ program from my Python GUI and I also want the counter 0 to 9 to be displayed live on a python widget, for example a display progress bar.
Fruny
Fruny
Look at the os.popen functions. Probably os.popen2().
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Radiocreed
Radiocreed
I tried the os.popen method before, it captures all the output from the standard out, it's not live. I want to do it on the fly.
Kylotan
Kylotan
To be honest I don't think you'll find many people willing to attempt to do what you're doing - you're hoping for a translation layer to read the standard output from one program and display that on a widget which is somewhat unwieldy and unusual. What you would more commonly use is some sort of loop in the Python program itself, which generates the number and modifies the widget directly.

However, since the example you gave seems very contrived, I get the feeling you are either trying to do something more complex (and this is a cut-down example) or you are asking about interfacing between C and PyGTK in general. Perhaps more information could help us give you a better answer.
Radiocreed
Radiocreed
Well, I was wondering if I could write a C++ function before or after each printf function which will somehow communicate to a python widget(a display progress bar in this case). The widget will then listen to any changes in value from this function and display the progress accordingly.

Doing such thing when the GUI and main source code are within the same language is possible, but I'm not sure if it's possible with Python and C++.
Fruny
Fruny
Quote:
Original post by Radiocreed
I tried the os.popen method before, it captures all the output from the standard out, it's not live. I want to do it on the fly.


Yes, it *is* live. The data is available on the pipe as soon as it is written out (unless you're still using a single-task OS like MS-DOS). It is up to you to a) write out the data in a format that you can easily process at the other end and b) read it in a timely fashion if you want timely updates. Like it or not, if your program sends all of its output over a single channel (stdout)... you will have to deal with it all. There are no ways, short of modifying the C program, to intercept individual printfs. This is, incidentally, why there are separate channels for program output (stdout) and for error reporting (stderr) - you don't want the error messages to pollute your output, and you want to be able to log them.

Quote:
Doing such thing when the GUI and main source code are within the same language is possible, but I'm not sure if it's possible with Python and C++.


Well, if you *are* willing to modify the program, you can have the python and C programs communicate over a socket (either a Unix-domain socket, or an INET socket over the loopback interface (127.0.0.1)). Alternatively, you can open a pipe, fork/exec (or spawn on win32) the child (C) process, and communicate over that pipe. os.popen2 is simply more convenient because it directly connects the standard file descriptors of the child process to the pipe, which is generally what you want.

And its not so much a question of having the GUI and main source code written in the same language rather than having them be in the same *process*. Many UNIX applications get GUIs added to them non-intrusively by adequate use of sockets. Of course UNIX apps are generally designed to make piping easy and have a low level of noise in their output (many programs remain completely silent unless they fail in some way).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Radiocreed
Radiocreed
Can I see a sample code on how you do it live with popen?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.