[Tkinter-discuss] Creating a new widget class

Peter Milliken peter.milliken at gmail.com
Thu Aug 14 23:36:40 CEST 2008


Hi Fred,

Yeah, this question hit my "curiousity bump" and I have been "investigating"
it on and off - as time allows :-)

I "discovered" what you have detailed - the last Tk object "bound" is the
one that tries to interpret the call (via self._w) - and if that happened to
be the Scrollbar then the Listbox methods become "lost" for want of a better
term - and vice versa.

I then started to look at "intercepting" the unsuccessful call i.e. before
the exception is raised, to see if I could potentially "re-route" the
request at run-time - but couldn't work out how to do that, I suspect you
can't?

Peter

On Wed, Aug 13, 2008 at 7:07 PM, Fredrik Lundh <fredrik at pythonware.com>wrote:

> Peter Milliken wrote:
>
>  Well, here are some clues - the "multiple" inheritance doesn't seem to
>> work the way the manual *implies* it should i.e. multiple inheritance is
>> described as searching for methods in the first inheritance and then all
>> it's ancestors, then onto the next and all its ancestors and so on.
>>
>
> Tkinter widget instances are proxy objects that delegate method calls to an
> underlying Tk widget.  A widget instance can only be bound to a single Tk
> widget.
>
> Multiple inheritance works as usual at the Tkinter class level, but that
> doesn't help if the widget instance is not bound to the right Tk object when
> you do a method call.
>
>  To get an "insert" (Listbox) method on your class you need to invoke the
>> __init__ method on the super classes (sub-classes should always invoke the
>> initialisation of their super class anyway - so that is something your class
>> definition is definitely missing!) i.e.
>>
>>   Listbox.__init__(self)
>>
>
> That binds the widget to the underlying Tk listbox widget (see the code in
> Tkinter's BaseWidget for details).  After this call, any method calls will
> be forwarded to the Tk listbox.
>
> >    Listbox.__init__(self)
> >    Scrollbar.__init__(self)
> >
> > And viola! ScrolledList now has the methods of Scrollbar - and "lost" >
> the methods of Listbox.
>
> After those calls, the widget instance ends up being bound to a Tk
> scrollbar widget.  There's no change in what methods the object has at the
> Python level, though.
>
> </F>
>
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20080815/0b7f8b4a/attachment.htm>


More information about the Tkinter-discuss mailing list