OOP and Tkinter

Diez B. Roggisch deets at nospam.web.de
Mon May 15 14:53:29 EDT 2006


Ronny Mandal schrieb:
> Hello.
> 
> I am stuck; provided is the code and error-msg:
> 
> file front_ui.py:
> 
> class Front(object):
>     _images = [] # Holds image refs to prevent GC
>     def __init__(self, root):
> 
> 
>         # Widget Initialization
>         self._listbox_1 = Tkinter.Listbox(root,
>             height = 0,
>             width = 0,
> 	...
> 	)
> 
> 
> 
> 
> 
> 
> other file:
> 
> from Front_ui import Front
> 
> class CustomFront(Front):
> 
> 
>      Front._listbox_1.insert( 0, 'foo' ) 
> 
> ...
> ...
>   File "H:\My Documents\Komodo\Front.py", line 63, in CustomFront
>     Front._listbox_1.insert( 0, foo' )
> AttributeError: type object 'Front' has no attribute '_listbox_1'
> 
> 
> i.e., it cannot find the listbox! Strange, both files is in the same
> folder. What is wrong here?

That you refer to the type/class Front with expressions like Front.<...> 
instead to an _instance_ of Front. You need to do things like this:

class CustomFront(Front):
      def __init__(self, root):
          super(CustomFront, self).__init__(root) #now the super-classes 
instance variables are there!
          self._listbox_1.insert( 0, 'foo' )




Diez



More information about the Python-list mailing list