OOP and Tkinter

Discussion in 'Python' started by Ronny Mandal, May 15, 2006.

  1. Ronny Mandal

    Ronny Mandal Guest

    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
    Ronny Mandal, May 15, 2006
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, May 15, 2006
    #2
    1. Advertising

  3. Ronny Mandal

    Ronny Mandal Guest

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


    -Ronny M
    Ronny Mandal, May 15, 2006
    #3
  4. Ronny Mandal

    Kent Johnson Guest

    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
    Kent Johnson, May 15, 2006
    #4
  5. 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
    Diez B. Roggisch, May 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    488
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    438
    Jeff Epler
    Aug 23, 2004
  3. 3c273

    Tkinter (OOP?) help

    3c273, Dec 30, 2004, in forum: Python
    Replies:
    2
    Views:
    450
    3c273
    Jan 3, 2005
  4. Hidekazu IWAKI
    Replies:
    1
    Views:
    361
    Peter Otten
    Dec 14, 2009
  5. Hidekazu IWAKI
    Replies:
    0
    Views:
    512
    Hidekazu IWAKI
    Dec 15, 2009
Loading...

Share This Page