Code without effect (wx demo TreeCtrl.py ImageList)

Discussion in 'Python' started by Martin Drautzburg, Dec 20, 2004.

  1. In the wx demoy TreeCtrl.py I find the following code, that should
    have no effect but seems to be needed nevertheless.

    class TestTreeCtrlPanel(wx.Panel):
    def __init__(self, parent, log):
    [...}
    self.tree = MyTreeCtrl(self, tID, wx.DefaultPosition, ...
    isz = (16,16)
    il = wx.ImageList(isz[0], isz[1])
    fldridx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART...
    [...]

    self.tree.SetImageList(il)
    --> self.il = il

    What is the effect of the last statement? self.il is not used
    anywhere. I used similar code in my application and it crashes unless
    I assign the image list to the parent panel. The name of the attribute
    does not seem to matter. I can write self.foo=il just as well, but
    without it it crashes.
    Martin Drautzburg, Dec 20, 2004
    #1
    1. Advertising

  2. Martin Drautzburg

    Steve Holden Guest

    Martin Drautzburg wrote:

    > In the wx demoy TreeCtrl.py I find the following code, that should
    > have no effect but seems to be needed nevertheless.
    >
    > class TestTreeCtrlPanel(wx.Panel):
    > def __init__(self, parent, log):
    > [...}
    > self.tree = MyTreeCtrl(self, tID, wx.DefaultPosition, ...
    > isz = (16,16)
    > il = wx.ImageList(isz[0], isz[1])
    > fldridx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART...
    > [...]
    >
    > self.tree.SetImageList(il)
    > --> self.il = il
    >
    > What is the effect of the last statement? self.il is not used
    > anywhere. I used similar code in my application and it crashes unless
    > I assign the image list to the parent panel. The name of the attribute
    > does not seem to matter. I can write self.foo=il just as well, but
    > without it it crashes.


    You will probably find that without that reference to the image list it
    will be garbage-collected while the images are still beign displayed,
    leading to results that are at best unpredictable and at worst fatal to
    your program.

    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
    Steve Holden, Dec 20, 2004
    #2
    1. Advertising

  3. Martin Drautzburg

    Paul McGuire Guest

    "Martin Drautzburg" <> wrote in message
    news:...
    > In the wx demoy TreeCtrl.py I find the following code, that should
    > have no effect but seems to be needed nevertheless.
    >
    > class TestTreeCtrlPanel(wx.Panel):
    > def __init__(self, parent, log):
    > [...}
    > self.tree = MyTreeCtrl(self, tID, wx.DefaultPosition, ...
    > isz = (16,16)
    > il = wx.ImageList(isz[0], isz[1])
    > fldridx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART...
    > [...]
    >
    > self.tree.SetImageList(il)
    > --> self.il = il
    >
    > What is the effect of the last statement? self.il is not used
    > anywhere. I used similar code in my application and it crashes unless
    > I assign the image list to the parent panel. The name of the attribute
    > does not seem to matter. I can write self.foo=il just as well, but
    > without it it crashes.


    Two possibilities come to mind:
    - used by a base class wx.Panel for which you do not have the source
    (although if this were the case, I would think the attribute name would be
    significant)
    - needed to retain a reference handle to the image list, to prevent it from
    being garbage collected (kind of far-fetched, but consistent with the "name
    it anything you want" behavior)

    -- Paul
    Paul McGuire, Dec 20, 2004
    #3
    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. Peter Ho
    Replies:
    2
    Views:
    10,318
    Carl Prothman [MVP]
    Jul 26, 2003
  2. Herr Lucifer
    Replies:
    0
    Views:
    540
    Herr Lucifer
    Jun 21, 2004
  3. Steve R.
    Replies:
    7
    Views:
    1,599
    Whitecrest
    Feb 17, 2004
  4. F. GEIGER
    Replies:
    9
    Views:
    1,201
    F. GEIGER
    May 3, 2004
  5. Andy Leszczynski

    wxPython demo /Process does not open new demo

    Andy Leszczynski, Feb 18, 2005, in forum: Python
    Replies:
    1
    Views:
    634
    Andy Leszczynski
    Feb 18, 2005
Loading...

Share This Page