OOP and Tkinter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ronny Mandal

    #1

    OOP and Tkinter

    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(Fro nt):


    Front._listbox_ 1.insert( 0, 'foo' )

    ....
    ....
    File "H:\My Documents\Komod o\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
  • Diez B. Roggisch

    #2
    Re: OOP and Tkinter

    Ronny Mandal schrieb:[color=blue]
    > 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(Fro nt):
    >
    >
    > Front._listbox_ 1.insert( 0, 'foo' )
    >
    > ...
    > ...
    > File "H:\My Documents\Komod o\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?[/color]

    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(Fro nt):
    def __init__(self, root):
    super(CustomFro nt, self).__init__( root) #now the super-classes
    instance variables are there!
    self._listbox_1 .insert( 0, 'foo' )




    Diez

    Comment

    • Ronny Mandal

      #3
      Re: OOP and Tkinter


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


      -Ronny M

      Comment

      • Kent Johnson

        #4
        Re: OOP and Tkinter

        Ronny Mandal wrote:[color=blue]
        > 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,
        > ...
        > )
        >[/color]
        [color=blue]
        > other file:
        >
        > from Front_ui import Front
        >
        > class CustomFront(Fro nt):
        > Front._listbox_ 1.insert( 0, 'foo' )
        >
        > ...
        > ...
        > File "H:\My Documents\Komod o\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?[/color]

        _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

        Comment

        • Diez B. Roggisch

          #5
          Re: OOP and Tkinter

          Ronny Mandal schrieb:[color=blue]
          > Thanks, but the problem was my undentation, after making one indent,
          > it worked as expected. :)[/color]

          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


          Comment

          Working...