Interaction between TclTk editor with Python code

Jeff Epler jepler at unpythonic.net
Tue May 17 07:56:20 EDT 2005


One way to get a handle on some Tcl variables from Python is to create
Tkinter.Variable instances with the names of the existing Tcl variables
(normally, the variable names are chosen arbitrarily).  Once you've done
this, you can do things like add variable traces (the trace_variable
method) on the Python side.  One thing to be aware of is that the Python
object's __del__ will unset the variable on the Tcl side.  Also, this
code sets an initial value for the variable which may or may not be
appropriate in your application.

The code below was ripped from a larger application, so it may or may
not work without modification.

Jeff

# This code is in the public domain
from Tkinter import *
def makevar(master, name, klass, *default):
    self = newinstance(klass)
    self._master = master
    self._tk = master.tk
    self._name = name
    if default:
        self.set(default[0])
    else:
        self.set(self._default)
    return self

def makebool(master, name, *default):
    return makevar(master, name, Tkinter.BooleanVar, *default)
def makeint(master, name, *default):
    return makevar(master, name, Tkinter.IntVar, *default)
def makefloat(master, name, *default):
    return makevar(master, name, Tkinter.DoubleVar, *default)
def makestring(master, name, *default):
    return makevar(master, name, Tkinter.StringVar, *default)
-------------- 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/20050517/18b46d22/attachment.sig>


More information about the Python-list mailing list