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

David King dking at nrao.edu
Fri Apr 29 19:01:15 CEST 2005

> David King wrote:
>> I am looking to make a custom Tcl/Tk widget available in Tkinter.  It 
>> is written in C/C++ and does graphics rendering of some custom 
>> astronomical data formats.  It has previously been driven stand-alone 
>> from a C++ program, without benefit of python (i.e., the Tcl/Tk 
>> interpreter and its event loop are currently created / run from the 
>> top level of a C++ program).

Fredrik Lundh wrote:

> are you migrating from C++ to Python, and have full control over the code
> base (including the widget)?  if so, the approach outlined here might be 
> less
> work:
>    http://effbot.org/zone/tkinter3000-embed.htm
> for configuration and custom events, use standard Python extension 
> techniques:
>    http://docs.python.org/ext/simpleExample.html
>    http://docs.python.org/ext/callingPython.html

Thank you, Mr. Lundh, for your helpful pointers.  I was especially gratified 
to see that the standard python C API had routines for calling back to 
python code from C[++].

We are fairly new to the python world here, but several of us are 
investigating the various 'binding systems' (boost, esp.; also swig, sip) 
that purport to make python-C++ communication simpler than python's standard 
(PyObject*) offerings; any comments you have on these will be welcome, 
including whether the callbacks to python that you mention have any 
shortcuts in, say, boost.

The 'Tkinter 3000 Widget Construction Kit' looks interesting, although to 
first appearance it appears designed mainly for implementing new widgets 
purely in python.  Our 'display library' primarily draws fancy graphics 
straight to the X window underlying its incarnation as a Tk widget. 
Although we control all this code, we definitely don't plan to reimplement 
the graphics part of the library entirely in python (just the GUIs 
surrounding the core graphics widget).  If, however there are ways in which 
WCK can create the 'widget' itself, but pass out to the C++ code the handles 
it needs to do its drawing (X window, gc, display, etc.), perhaps this 
approach may be workable.  Any advice in that regard is welcome.

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.

Once I've got that handle, the way a custom Tk widget is created in C++ is 
to register my widget 'constructor' and other widget operations with the 
interpreter as new Tcl/Tk commands.  E.g., after such registration calls (to 
'Tcl_CreateCommand' etc.), the arbitrarily-chosen new name 'pixelcanvas' 
could now be given as a new Tcl/Tk command that will construct an instance 
of my custom widget.  I'm wondering how this new 'pixelcanvas' command (and 
any others I register to operate the widget) are made available to be 
invoked from python/Tkinter.

Any further insights you have in this regard will be welcome.

Thanks again,

David King

More information about the Tkinter-discuss mailing list