Embedding python into PyQt

Discussion in 'Python' started by Grzegorz Dostatni, Apr 30, 2004.

  1. 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)
     
    Grzegorz Dostatni, Apr 30, 2004
    #1
    1. Advertising

  2. > 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.
    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, May 1, 2004
    #2
    1. Advertising

  3. Grzegorz Dostatni

    Jody Winston Guest

    "Diez B. Roggisch" <> writes:

    >> 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.
    > --
    > Regards,
    >
    > Diez B. Roggisch


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

    """Based on:

    http://groups.google.com/groups?q=&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())


    --
    Jody Winston
     
    Jody Winston, May 3, 2004
    #3
  4. 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)


    On Fri, 30 Apr 2004, Grzegorz Dostatni wrote:

    >
    > 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)
    >
    >
    >
    >
     
    Grzegorz Dostatni, May 3, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulysses
    Replies:
    4
    Views:
    823
    Werner Schiendl
    Oct 22, 2003
  2. davout
    Replies:
    0
    Views:
    333
    davout
    Aug 4, 2004
  3. John Dean
    Replies:
    2
    Views:
    744
    Roman Yakovenko
    Feb 5, 2006
  4. Kenneth McDonald
    Replies:
    0
    Views:
    668
    Kenneth McDonald
    Dec 19, 2006
  5. Girish
    Replies:
    1
    Views:
    469
    Tim Golden
    May 31, 2007
Loading...

Share This Page