stdin in embedded python

Gabriel Genellina gagsl-py2 at
Sun Nov 1 22:02:03 CET 2009

En Sun, 01 Nov 2009 13:34:44 -0300, KillSwitch <gu.yakahughes at>  
> On Nov 1, 5:34 am, Dave Angel <da... at> wrote:
>> KillSwitch wrote:

>> > I have a C++ program, with a GUI, into which I have embedded python. I
>> > have made several python functions in C++, one of which I use to
>> > override the normal stdout and stderr so that they print to a text box
>> > of my GUI. One thing I cannot think of how to do is to redefine stdin
>> > so that it pauses the program, waits for a user to type input into the
>> > box, hit enter, and takes input from another text element and sends it
>> > to python like it was the console.
>> I suspect you don't really want to redirect stdin, but instead implement
>> raw_input(). [...]Try changing __builtins__.raw_input  to reference  
>> your new
>> function.
> But what would the function do? How would it pause python and wait for
> it to have text to send?

Whatever you want. You don't have to "pause python", Python itself won't  
resume until your function doesn't return. (You should release the GIL if  
your C++ function doesn't call back to Python code, to allow other threads  
to continue, but that's another story).
This is a raw_input replacement written in Tkinter; it shows a dialog box  
instead of reading from stdin:

py> from Tkinter import *
py> from tkSimpleDialog import askstring
py> def my_raw_input(prompt):
...   return askstring("Python", prompt)
py> root = Tk()
py> import __builtin__
py> __builtin__.raw_input = my_raw_input
py> raw_input("What's your name?")

Gabriel Genellina
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ClipBoard-1.png
Type: image/png
Size: 5666 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list