[Tkinter-discuss] Creating a new widget class

Fredrik Lundh fredrik at pythonware.com
Wed Aug 13 11:07:45 CEST 2008


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>



More information about the Tkinter-discuss mailing list