importing a tcl/tk widget

Fredrik Lundh fredrik at pythonware.com
Wed Nov 7 01:51:14 CET 2001


"Jeffrey" wrote
> I know that you can write a Tkinter wrapper function but I don't
> really know how to do this.

assuming you mean what you wrote, it's not that hard to
write wrappers for new widgets (the hard part is probably
to link them into Tkinter).

here's a minimal wrapper which wraps a small portion of the
standard Entry widget.

import Tkinter

class Entry(Tkinter.Widget):
    def __init__(self, master=None, cnf={}, **kw):
        Tkinter.Widget.__init__(self, master, "entry", cnf, kw)
    def delete(self, first, last=None):
        self.tk.call(self._w, "delete", first, last)
    def get(self):
        return self.tk.call(self._w, "get")
    def index(self, index):
        return int(self.tk.call(self._w, "index", index))
    def insert(self, index, string):
        self.tk.call(self._w, "insert", index, string)

:::

details:

the Tkinter.Widget constructor takes a master (None for the
default window), the name of the Tk widget, and two dictionaries
(which provide options; they're merged inside the constructor).
the constructor takes the widget name, the master, and the
options, and turn that into a Tcl command:

    entry .1020213 -option value -option value

self._w is the Tk name for the widget (set by the constructor).
it's usually a sequence of dot and obscure integers.

the self.tk.call method takes one or more arguments, and execute
the corresponding Tcl command:

    self.tk.call(self._w, "get")

is mapped to a Tcl command looking something like:

    .1020213.3221021 get

tuples are converted to Tk lists (recursively), None is ignored, and
all other types are converted to string arguments.

that's it.

:::

or maybe you're just looking for the create_window function.  see
laura's answer for details.

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list