OOP and Tkinter

R

Ronny Mandal

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
 
D

Diez B. Roggisch

Ronny said:
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
 
R

Ronny Mandal

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


-Ronny M
 
K

Kent Johnson

Ronny said:
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
 
D

Diez B. Roggisch

Ronny said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top