[Tutor] Redirecting Python Interpreter Output

SA sarmstrong13@mac.com
Sat, 10 Aug 2002 08:57:02 -0500


> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3111814626_82358
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

On 8/10/02 6:15 AM, "Ketut Mahaindra" <KMahaindra@beijing.sema.slb.com>
wrote:

> Hi,=20
>=20
> I am currently developing an application
> which embed / extend Python in C++ / MSVC 6.0
>=20
> If I embed the Python interpreter in my C++ code, is there any way to
> redirect the output of the interpreter or the output of the script ?
> For example I want to catch the output and display it in an edit box
> Any suggestions here ?

I managed to do this with a little Python/Tkinter script. This is not the
best of scripts, but if you look in the =B3def expyth=B2 function you will see
what you are looking for. t1 is the input text box and t2 is the output tex=
t
box. This script is designed to run the python code in t1 and display the
output in t2.

#!/usr/bin/env python

from Tkinter import *
import os
import commands
import sys
import tkSimpleDialog

class PyShell:
   =20
    def clearin(self):
        self.t1.delete(0.0,END)
    def clearout(self):
        self.t2.delete(0.0,END)
    def expyth(self):
        self.t2.delete(0.0, END)  #Clear Textbox 2, ready it for input.
        self.output =3D commands.getoutput("/sw/bin/python -c '%s'" %
self.t1.get(0.0, END).strip())  #run commands from textbox1 and capture
their output.
        self.t2.insert(END, self.output) #write output to textbox2
    def doSave(self):
        SaveDialog =3D Saving(self.f)
        filename =3D SaveDialog.getName()
        self.saveText(filename)
        del(saveDialog)
    def __init__(self, top):
        self.t1 =3D Text(top, height=3D"12", width=3D"84", font=3D"Courier 12")
        self.t1.pack(side=3DTOP, pady=3D2)
        self.f =3D Frame(top)
        self.f.pack()
        self.b1 =3D Button(self.f, text=3D"Execute", command=3Dself.expyth)
        self.b1.pack(side=3DLEFT)
        self.b2 =3D Button(self.f, text=3D"Clear Input", command=3Dself.clearin)
        self.b2.pack(side=3DLEFT)
        self.b3 =3D Button(self.f, text=3D"Clear Output", command=3Dself.clearout=
)
        self.b3.pack(side=3DLEFT)
        self.b4 =3D Button(self.f, text=3D"Save Input", command=3Dself.doSave)
        self.b4.pack(side=3DLEFT)
        self.t2 =3D Text(top, height=3D"12", width=3D"84", bg=3D"lightblue",
font=3D"Courier 12")
        self.t2.pack(side=3DTOP, pady=3D2)

class Saving(tkSimpleDialog.Dialog):
   =20
    def body(self, master):
        Label(master, text=3D"Directory:").grid(row=3D0)
        Label(master, text=3D"Filename:").grid(row=3D1)
        self.e1 =3D Entry(master)
        self.e2 =3D Entry(master)
        self.e1.grid(row=3D0, column=3D1)
        self.e2.grid(row=3D1, column=3D1)
        return self.e1
    def apply(self):
        dir =3D self.e1.get()
        fn =3D self.e2.get()
        self.name =3D dir + fn
    def getName(self):
        return self.name

                  =20
root =3D Tk()
app =3D PyShell(root)
root.mainloop()


Hope this help you.
SA


--=20
"I can do everything on my Mac I used to on my PC. Plus a lot more ..."
-Me


--B_3111814626_82358
Content-type: text/html; charset="US-ASCII"
Content-transfer-encoding: quoted-printable

<HTML>
<HEAD>
<TITLE>Re: [Tutor] Redirecting Python Interpreter Output</TITLE>
</HEAD>
<BODY>
<FONT FACE=3D"Verdana">On 8/10/02 6:15 AM, &quot;Ketut Mahaindra&quot; &lt;KM=
ahaindra@beijing.sema.slb.com&gt; wrote:<BR>
<BR>
</FONT><BLOCKQUOTE><FONT FACE=3D"Verdana"><FONT SIZE=3D"2">Hi,</FONT> <BR>
<BR>
<FONT SIZE=3D"2">I am currently developing an application <BR>
which embed / extend Python in C++ / MSVC 6.0</FONT> <BR>
<BR>
<FONT SIZE=3D"2">If I embed the Python interpreter in my C++ code, is there a=
ny way to</FONT> <BR>
<FONT SIZE=3D"2">redirect the output of the interpreter or the output of the =
script ?</FONT> <BR>
<FONT SIZE=3D"2">For example I want to catch the output and display it in an =
edit box</FONT> <BR>
<FONT SIZE=3D"2">Any suggestions here ?</FONT> <BR>
</FONT></BLOCKQUOTE><FONT FACE=3D"Verdana"><BR>
I managed to do this with a little Python/Tkinter script. This is not the b=
est of scripts, but if you look in the &#8220;def expyth&#8221; function you=
 will see what you are looking for. t1 is the input text box and t2 is the o=
utput text box. This script is designed to run the python code in t1 and dis=
play the output in t2.<BR>
<BR>
#!/usr/bin/env python<BR>
<BR>
from Tkinter import *<BR>
import os<BR>
import commands<BR>
import sys<BR>
import tkSimpleDialog<BR>
<BR>
class PyShell:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def clearin(self):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t1.delete(0.0,END)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def clearout(self):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t2.delete(0.0,END)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def expyth(self):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t2.delete(0.0, END) &n=
bsp;#Clear Textbox 2, ready it for input.<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.output =3D commands.geto=
utput(&quot;/sw/bin/python -c '%s'&quot; % self.t1.get(0.0, END).strip()) &n=
bsp;#run commands from textbox1 and capture their output.<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t2.insert(END, self.ou=
tput) #write output to textbox2<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def doSave(self):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SaveDialog =3D Saving(self.f)=
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename =3D SaveDialog.getNa=
me()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.saveText(filename)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;del(saveDialog)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, top):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t1 =3D Text(top, height=3D=
&quot;12&quot;, width=3D&quot;84&quot;, font=3D&quot;Courier 12&quot;)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t1.pack(side=3DTOP, pady=
=3D2)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.f =3D Frame(top)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.f.pack()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b1 =3D Button(self.f, te=
xt=3D&quot;Execute&quot;, command=3Dself.expyth)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b1.pack(side=3DLEFT)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b2 =3D Button(self.f, te=
xt=3D&quot;Clear Input&quot;, command=3Dself.clearin)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b2.pack(side=3DLEFT)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b3 =3D Button(self.f, te=
xt=3D&quot;Clear Output&quot;, command=3Dself.clearout)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b3.pack(side=3DLEFT)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b4 =3D Button(self.f, te=
xt=3D&quot;Save Input&quot;, command=3Dself.doSave)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.b4.pack(side=3DLEFT)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t2 =3D Text(top, height=3D=
&quot;12&quot;, width=3D&quot;84&quot;, bg=3D&quot;lightblue&quot;, font=3D&quot;C=
ourier 12&quot;)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t2.pack(side=3DTOP, pady=
=3D2)<BR>
<BR>
class Saving(tkSimpleDialog.Dialog):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def body(self, master):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Label(master, text=3D&quot;Di=
rectory:&quot;).grid(row=3D0)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Label(master, text=3D&quot;Fi=
lename:&quot;).grid(row=3D1)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.e1 =3D Entry(master)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.e2 =3D Entry(master)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.e1.grid(row=3D0, column=3D=
1)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.e2.grid(row=3D1, column=3D=
1)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.e1<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def apply(self):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dir =3D self.e1.get()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn =3D self.e2.get()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name =3D dir + fn<BR>
&nbsp;&nbsp;&nbsp;&nbsp;def getName(self):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.name<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
root =3D Tk()<BR>
app =3D PyShell(root)<BR>
root.mainloop()<BR>
<BR>
<BR>
Hope this help you.<BR>
SA<BR>
<BR>
<BR>
-- <BR>
&quot;I can do everything on my Mac I used to on my PC. Plus a lot more ...=
&quot;<BR>
-Me<BR>
</FONT>
</BODY>
</HTML>


--B_3111814626_82358--