Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > OOP and Tkinter

Reply
Thread Tools

OOP and Tkinter

 
 
Ronny Mandal
Guest
Posts: n/a
 
      05-15-2006
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?


Thanks,

Ronny Mandal
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-15-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
 
Reply With Quote
 
 
 
 
Ronny Mandal
Guest
Posts: n/a
 
      05-15-2006

Thanks, but the problem was my undentation, after making one indent,
it worked as expected.


-Ronny M
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      05-15-2006
Ronny Mandal wrote:
> 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?


_listbox_1 is an instance attribute, not a class attribute. You need to
refer to self._listbox_1 from a CustomFront method, or change _listbox_1
to a class attribute if that is what you really want.

Kent
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-15-2006
Ronny Mandal schrieb:
> Thanks, but the problem was my undentation, after making one indent,
> it worked as expected.


I seriously doubt that. I guess you didn't show us the real code. But
there is no way that an instance variable can be accessed on type-level
in a derived class (or from anywhere, for that matter). So I presume you
have some misconceptions about pythons OO-model that will sooner or
later bite you - for example if you want to have several Front-objects.

Diez


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the differences between tkinter in windows and Tkinter inthe other platform? Hidekazu IWAKI Python 0 12-15-2009 05:58 AM
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
Tkinter (OOP?) help 3c273 Python 2 01-03-2005 03:41 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments