Attaching to a Python Interpreter a la Tcl

Jeff Epler jepler at unpythonic.net
Thu Feb 24 08:49:47 EST 2005


Cameron Laird mentioned Tk's send working with Python; if you are writing your
app with Tkinter, here is some code to let you use tcl commands like
    send <appname> python <expression or statement>
for remote control.  You could build a more sophisticated front-end for this,
and you'll probably also want to add stuff like sending the text of an
exception as the result of the 'send' command.

Jeff

#------------------------------------------------------------------------
import Tkinter

__all__ = 'python', 'setup_send'
def makecommand(master, name, func, subst=None, needcleanup=0):
    f = Tkinter.CallWrapper(func, subst, master).__call__
    master.tk.createcommand(name, f)
    if needcleanup:
        if master._tclCommands is None:
            master._tclCommands = []
        master._tclCommands.append(name)
    return name


def setup_send(app, ns, name="python"):
    def python(*args):
        s = " ".join(args)
        print args
        try:
            code = compile(s, '<send>', 'eval')
            return eval(code, ns)
        except SyntaxError:
            code = compile(s, '<send>', 'exec')
            exec code in ns
    makecommand(app, name, python)

if __name__ == '__main__':
    app = Tkinter.Tk()
    setup_send(app, {})
    app.mainloop()
#------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050224/19221bbc/attachment.sig>


More information about the Python-list mailing list