Embedding python into PyQt

G

Grzegorz Dostatni

Cheers.
What I am trying to do is to embed the python interactive interpreter
inside a qt TextEdit.

I could write an event loop myself, but I was wandering if there exists a
solution somewhere, or the best way to do it myself.

Greg

Advice is what we ask for when we already know the answer but wish we
didn't.
-- Erica Jong (How to Save Your Own Life, 1977)
 
D

Diez B. Roggisch

What I am trying to do is to embed the python interactive interpreter
inside a qt TextEdit.

I could write an event loop myself, but I was wandering if there exists a
solution somewhere, or the best way to do it myself.

I'm quite sure this has been discussed on the pykde mailing list a while ago
- search the archines.

A program that actually does do that is eric3.
 
J

Jody Winston

Diez B. Roggisch said:
I'm quite sure this has been discussed on the pykde mailing list a while ago
- search the archines.

A program that actually does do that is eric3.

"""mainloop.py -- a nearly exact imitation of the Python main loop."""

"""Based on:

http://groups.google.com/[email protected]&rnum=2
"""

import traceback, sys

def run(code, env):
try:
exec code in env
except:
sys.last_type = sys.exc_type
sys.last_value = sys.exc_value
sys.last_traceback = sys.exc_traceback
traceback.print_exception(sys.last_type,
sys.last_value,
sys.last_traceback)

def mainloop(env):

print "Fake Python", sys.version
# print sys.copyright
print 'Type "help", "copyright", "credits" or "license" for more information.'

# Set sys.ps1 and sys.ps2 if they are undefined;
# they are only set in interactive mode
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "

# Source collected so far; empty if at start of statement
source = ""

while 1:
if source:
prompt = sys.ps2
else:
prompt = sys.ps1

try:
line = raw_input(prompt)
except EOFError:
break
except KeyboardInterrupt:
source = ""
print "\nKeyboardInterrupt"
continue

if source:
source = source + "\n" + line
else:
source = line

# Compile three times: as is, with \n, and with \n\n appended.
# If it compiles as is, it's complete. If it compiles with
# one \n appended, we expect more. If it doesn't compile
# either way, we compare the error we get when compiling with
# \n or \n\n appended. If the errors are the same, the code
# is broken. But if the errors are different, we expect more.
# Not intuitive; not even guaranteed to hold in future
# releases; but this matches the compiler's behavior in Python
# 1.4 and 1.5.

err = err1 = err2 = None
code = code1 = code2 = None

try:
code = compile(source, "<input>", "single")
except SyntaxError, err:
pass

try:
code1 = compile(source + "\n", "<input>", "single")
except SyntaxError, err1:
pass

try:
code2 = compile(source + "\n\n", "<input>", "single")
except SyntaxError, err2:
pass

## print code, code1, code2
## print 'err = "%s"' % (err)
## print 'err1 = "%s"' % (err1)
## print 'err2 = "%s"' % (err2)
## print "same = ", err1 == err2

if code:
## print "We got code"
run(code, env)
source = ""
elif code1:
## print "We got code1"
pass
elif str(err1) == str(err2):
## print "err1 == err2"
traceback.print_exception(SyntaxError, err1, None)
source = ""

if __name__ == '__main__':
mainloop(globals())
 
G

Grzegorz Dostatni

I am going to reply to my own message. That way, if someone else looks for
something similar - it'll be in the archives.

PyCute is part of a PyQwt project (available at sourceforge). It does
exactly what I want. I am using the PyCute3.py.

There is a small snag if you want to use it with a Qt designer. I solved
it by creating the following file: PyCute.py
---------------------------------------------------------
import PyCute3


class PyCute(PyCute3.PyCute):
def __init__(self, parent, name):
PyCute3.PyCute.__init__(self, parent=parent)
---------------------------------------------------------

After having that and the PyCute3.py in the same directory, just add a
custom widget to designer as class PyCute. and include the line:

Python:from PyCute import PyCute

in the comment field of the form settings.

Greg

Advice is what we ask for when we already know the answer but wish we
didn't.
-- Erica Jong (How to Save Your Own Life, 1977)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top