Inherit Class Dynamicly

Laura Creighton lac at strakt.com
Tue Feb 5 16:06:25 CET 2002


> Hi, everybody,
> 
> from Tkinter import *
> class NewWidget:
>         ...
>         def __init__(self, widget_name = "Button", **dict):
>                 # What is needed here to make the new born object(self) 
>                 # behavior like both the class specified by widget_name
>                 # and NewWidget?
> 
> Thanks from
> 
> Xiao-Qin
> 

I do it like this:

import Tkinter
class MyButton(Tkinter.Button): # single
    """
    This is a simple wrapper for the Tkinter Button, which just makes
    an alias, 'data' for 'text' in the button.  
    """
    def __init__(self, parent, **kw):
        """
        Function name: __init__
        Description:   Initialise MyButton.
        Arguments:     parent - the parent frame,
                       **kw   - a dictionary containing all the option=value
                       pairs as {opt1:val1, opt2:val2 ...}  If you put
                       things in here which are not valid options to a
                       Tkinter.Button you must remember to del them
                       from the dictionary before you call
                       Tkinter.Button.__init_
        """
        self.parent = parent
        if kw.has_key('data'):
            kw['text'] = kw['data']
            del kw['data']
     
        Tkinter.Button.__init__(self, parent, **kw)
	
        #This assumes you have 2.0 or better.  If you have 1.5.2
        #apply(Tkinter.Button.__init__, (self, parent), kw)
     
    # go add some more methods here as part of MyButton.  Here is a
    # good one for me to add since I just made an alias.

    def cget(self, property):
        """
        Function name: cget
        Description:   Add return values for the additional attribute
                       data in case somebody wants to find it this way.
        Arguments:     None
        Returns:       the data that is contained in the widget
        """
        if property == 'data':
            return(Tkinter.Button.cget(self, 'text'))
        else:
            return(Tkinter.Button.cget(self, property))

###########################################################
if __name__ == '__main__':
    root = Tkinter.Tk()
    w = MyButton(root, width= 30, fg='green', 
        text='This is a very green button')
    widgetList = (
        ['show', lambda x = w: x.grid(row=0, col=0, columnspan=5)],
        ['pink', lambda x = w: x.configure(fg='pink')],
        ['yellow', lambda x = w: x.configure(bg='yellow')],
        ['Exit', root.destroy],
        )
    column = 0
    for txt, cmd in widgetList:
        button = Tkinter.Button(root, text=txt, command=cmd)
        button.grid(row=1, col=column, sticky='w')
        column += 1
    root.mainloop()

----------------------------------------------------
 If you want multiple inheritance, that is fine too, i.e.

 class MyButton(MyWidgetMixin, Tkinter.Button): 

   but somewhere in the __init__ you need to do this
   MyWidgetMixin.__init__(self, whatever_you_want)

---------------------------

Laura Creighton




More information about the Python-list mailing list