[Tkinter-discuss] Creating a new widget class

Ron Longo ron.longo at cox.net
Tue Aug 5 23:45:16 CEST 2008

There are two ways to create your own widgets with Tkinter: subclass from an 
existing widget or construct your new widget in a Frame.  Subclassing from a 
Frame is the more general method.  Subclassing an existing widget is usually 
only useful if you are creating a specialized version of an existing widget. 
If your widget is a composite of two or more existing widgets (often called 
a megawidget), you should almost always use the Frame approach. 
Alternatively you can use a widget-building framework such as PMW.

Here's how to get started:  In your constructor you first initialize the 
superclass then create your childwidgets.  The parent of the subwidgets is 

class ScrolledList( Tkinter.Frame ):
   def __init__( self, parent, **options ):
      Tkinter.Frame.__init__( self, parent, **options )

      self._list = Tkinter.List( self )
      self._scrollbar = Tkinter.Scrollbar( self )

      self._list.pack( side=Tkinter.LEFT )
      self._scrollbar.pack( side=Tkinter.LEFT )

This allows your widget to behave like any other widget.  That is to use 
your widget, you construct it:

   myWidgetInstance = ScrolledList( aParentWidget, ... )

Then you pack, grid or place it to display it.

   myWidgetInstance.pack( )

If you want to allow your widget to handle its own custom widget options, 
you need to pull these options out of the **options argument before calling 
Tkinter.Frame.__init__( ... ).

If you don't like the .pack() methods being in __init__, you could override 
pack(), grid() and place() and pack the list and scrollbar there instead.

Hope this helps

----- Original Message ----- 
From: "vtcodger" <donaldkenney at gmail.com>
To: <tkinter-discuss at python.org>
Sent: Sunday, August 03, 2008 5:34 PM
Subject: [Tkinter-discuss] Creating a new widget class

> I wonder if someone who understands Python and Tkinter a bit better than I
> could help me out.  Basically, I'm trying to encapsulate the kind of messy
> stuff associated with setting up a listbox with a scrollbar in a Python
> class.  From my probably incredibly naive point of view, it doesn't look 
> too
> hard.
> class ScrolledList(Frame,Listbox,Scrollbar) :
>  #At this point we have a template for objects that have all the
>  #attributes and methods of Frames,Listboxes,and Scrollbars.  For
>  #names that occur in all, the Frame version takes precedence over
>  #the Listbox version and that in turn takes precedence over
>  #Scrollbar version
>  def __init__ (self, Tkobject, height=4, width=50) :
>    #This code is executed whenever a new scrolled list object is
>    #created (instantiated)
>    self.f = Frame(Tkobject)
>    s = Scrollbar(self.f,orient=VERTICAL)
>    self.l = Listbox(self.f, height=height, width=width,
> yscrollcommand=s.set, exportselection=0)
>   #We have now created a frame, scrollbar and listbox
>    s.config(command=self.l.yview)
>    s.pack(side=RIGHT, fill=Y); self.l.pack()
>   #And configured the listbox and scrollbar to interact
> And it creates a Tkinter object with a gazillion attributes. 
> Unfortunately
> tk isn't one of them.  When I try to invoke the methods, I am informed 
> that
> the new object has no 'tk' attribute.  That's correct.  It doesn't.
> Apparently I have failed to call some necessary constructor.  But which?
> Maybe I'm close to having it right because if I create a grid attribute in
> the class and pass the parameters to the frame grid method, the scrolled
> listbox can be configured and displayed.
>  def grid(self,row=90,column=1,rowspan=5,columnspan=4,sticky=W) :
> self.f.grid(row=row,column=column,rowspan=rowspan,columnspan=columnspan,sticky=sticky)
> What am I doing wrong, or not doing right?
> -- 
> View this message in context: 
> http://www.nabble.com/Creating-a-new-widget-class-tp18802754p18802754.html
> Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss 

More information about the Tkinter-discuss mailing list