Code without effect (wx demo TreeCtrl.py ImageList)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Drautzburg

    #1

    Code without effect (wx demo TreeCtrl.py ImageList)

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

    class TestTreeCtrlPan el(wx.Panel):
    def __init__(self, parent, log):
    [...}
    self.tree = MyTreeCtrl(self , tID, wx.DefaultPosit ion, ...
    isz = (16,16)
    il = wx.ImageList(is z[0], isz[1])
    fldridx = il.Add(wx.ArtPr ovider_GetBitma p(wx.ART_FOLDER , wx.ART...
    [...]

    self.tree.SetIm ageList(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.
  • Steve Holden

    #2
    Re: Code without effect (wx demo TreeCtrl.py ImageList)

    Martin Drautzburg wrote:
    [color=blue]
    > In the wx demoy TreeCtrl.py I find the following code, that should
    > have no effect but seems to be needed nevertheless.
    >
    > class TestTreeCtrlPan el(wx.Panel):
    > def __init__(self, parent, log):
    > [...}
    > self.tree = MyTreeCtrl(self , tID, wx.DefaultPosit ion, ...
    > isz = (16,16)
    > il = wx.ImageList(is z[0], isz[1])
    > fldridx = il.Add(wx.ArtPr ovider_GetBitma p(wx.ART_FOLDER , wx.ART...
    > [...]
    >
    > self.tree.SetIm ageList(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.[/color]

    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

    Comment

    • Paul McGuire

      #3
      Re: Code without effect (wx demo TreeCtrl.py ImageList)

      "Martin Drautzburg" <martin.drautzb urg@web.de> wrote in message
      news:87llbtjujx .fsf@web.de...[color=blue]
      > In the wx demoy TreeCtrl.py I find the following code, that should
      > have no effect but seems to be needed nevertheless.
      >
      > class TestTreeCtrlPan el(wx.Panel):
      > def __init__(self, parent, log):
      > [...}
      > self.tree = MyTreeCtrl(self , tID, wx.DefaultPosit ion, ...
      > isz = (16,16)
      > il = wx.ImageList(is z[0], isz[1])
      > fldridx = il.Add(wx.ArtPr ovider_GetBitma p(wx.ART_FOLDER , wx.ART...
      > [...]
      >
      > self.tree.SetIm ageList(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.[/color]

      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


      Comment

      Working...