[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, "Ketut Mahaindra" <KM=
ahaindra@beijing.sema.slb.com> 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 “def expyth” 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>
<BR>
def clearin(self):<BR>
self.t1.delete(0.0,END)<BR>
def clearout(self):<BR>
self.t2.delete(0.0,END)<BR>
def expyth(self):<BR>
self.t2.delete(0.0, END) &n=
bsp;#Clear Textbox 2, ready it for input.<BR>
self.output =3D commands.geto=
utput("/sw/bin/python -c '%s'" % self.t1.get(0.0, END).strip()) &n=
bsp;#run commands from textbox1 and capture their output.<BR>
self.t2.insert(END, self.ou=
tput) #write output to textbox2<BR>
def doSave(self):<BR>
SaveDialog =3D Saving(self.f)=
<BR>
filename =3D SaveDialog.getNa=
me()<BR>
self.saveText(filename)<BR>
del(saveDialog)<BR>
def __init__(self, top):<BR>
self.t1 =3D Text(top, height=3D=
"12", width=3D"84", font=3D"Courier 12")<BR>
self.t1.pack(side=3DTOP, pady=
=3D2)<BR>
self.f =3D Frame(top)<BR>
self.f.pack()<BR>
self.b1 =3D Button(self.f, te=
xt=3D"Execute", command=3Dself.expyth)<BR>
self.b1.pack(side=3DLEFT)<BR>
self.b2 =3D Button(self.f, te=
xt=3D"Clear Input", command=3Dself.clearin)<BR>
self.b2.pack(side=3DLEFT)<BR>
self.b3 =3D Button(self.f, te=
xt=3D"Clear Output", command=3Dself.clearout)<BR>
self.b3.pack(side=3DLEFT)<BR>
self.b4 =3D Button(self.f, te=
xt=3D"Save Input", command=3Dself.doSave)<BR>
self.b4.pack(side=3DLEFT)<BR>
self.t2 =3D Text(top, height=3D=
"12", width=3D"84", bg=3D"lightblue", font=3D"C=
ourier 12")<BR>
self.t2.pack(side=3DTOP, pady=
=3D2)<BR>
<BR>
class Saving(tkSimpleDialog.Dialog):<BR>
<BR>
def body(self, master):<BR>
Label(master, text=3D"Di=
rectory:").grid(row=3D0)<BR>
Label(master, text=3D"Fi=
lename:").grid(row=3D1)<BR>
self.e1 =3D Entry(master)<BR>
self.e2 =3D Entry(master)<BR>
self.e1.grid(row=3D0, column=3D=
1)<BR>
self.e2.grid(row=3D1, column=3D=
1)<BR>
return self.e1<BR>
def apply(self):<BR>
dir =3D self.e1.get()<BR>
fn =3D self.e2.get()<BR>
self.name =3D dir + fn<BR>
def getName(self):<BR>
return self.name<BR>
<BR>
&nb=
sp; <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>
"I can do everything on my Mac I used to on my PC. Plus a lot more ...=
"<BR>
-Me<BR>
</FONT>
</BODY>
</HTML>
--B_3111814626_82358--