[Tkinter-discuss] Re: Hooking into custom Tcl/Tk Widgets from Tkinter (vis)

Jeff Epler jepler at unpythonic.net
Fri Apr 29 20:25:23 CEST 2005

On Fri, Apr 29, 2005 at 11:01:15AM -0600, David King wrote:
> But let's leave WCK aside and just talk about plain Tkinter for the moment. 
>  I'd like to understand how my custom C++/Tk widget could make itself known 
> in the usual Tkinter world.  On the C++ side, essentially all my Tcl/Tk API 
> calls interact with a tcl interpreter handle ('Tcl_Interp*').  Presumably I 
> need to get the one Tkinter uses, instead of creating an interpreter myself 
> (and also to let Tkinter handle the event loop, rather than doing _that_ 
> myself).  I'm wondering how I get this handle from Tkinter.

Here is some "C" code that I have used for this purpose.

// this code is in the public domain
static Tcl_Interp *get_interpreter(PyObject *tkapp) {
    long interpaddr;
    PyObject *interpaddrobj = PyObject_CallMethod(tkapp, "interpaddr", NULL);
    if(interpaddrobj == NULL) { return NULL; }
    interpaddr = PyInt_AsLong(interpaddrobj);
    if(interpaddr == -1) { return NULL; }
    return (Tcl_Interp*)interpaddr;

'PyObject *tkapp' is the thing returned by _tkinter.create(), and available as
the 'tk' attribute on all Tkinter widgets.

This is not foolproof, because passing in an object like
    class K:
        interpaddr = 0 
will soon lead to a crash.  But if you can trust the calling code to only pass in
"real" interpreters, it should work just fine.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20050429/5df5a7ad/attachment.pgp

More information about the Tkinter-discuss mailing list