[wxPython] Please help with ListCtrl & Image problem ...

Sorry for the repost, but I am down to the last part of my app, and no one
commented on why my code is giving me grief. I am trying to use the
wxLC_ICON style with the wxListCtrl, but the icons never display. Below is a
test code snippet. Anyone see what I am doing wrong?

from wxPython.wx import *

class CTest( wxFrame ):
   def __init__(self):
      wxFrame.__init__(self, NULL, -1, "test" )
      il = wxImageList(16, 16)
      idx1 = il.Add( wxBitmap('inboxs.bmp', wxBITMAP_TYPE_BMP) )
      list = wxListCtrl(self, -1, style = wxLC_ICON )
      list.SetImageList(il, wxIMAGE_LIST_NORMAL)
      list.InsertImageStringItem(0, "test label", idx1)

class CApp( wxApp ):
   def OnInit(self):
      frame = CTest()
      frame.Show(true)
      self.SetTopWindow(frame)
      return true

app = CApp(0)
app.MainLoop()

Sorry for the repost, but I am down to the last part of my app, and no one
commented on why my code is giving me grief. I am trying to use the
wxLC_ICON style with the wxListCtrl, but the icons never display. Below is

a

test code snippet. Anyone see what I am doing wrong?

You need to save a reference to the image list. Since the same image list
can be used by more than one control, they don't take ownership of it and
delete it when done. That means that you need to save a reference to it
otherwise it is getting deleted when it goes out of scope.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

Robin Dunn discussed an issue with wxImageList (Dec 23, 1999) that causes the images to get garbage-collected prematurely. He proposed a work-around in maintaining a reference to the wxImageList to prevent it from getting garbage-collected. However, not only is it necessary to maintain a reference to the list itself, but also to the window that contains the list.

Below is a program that demonstrates the bug. The workaround is to use

def OnInit(self):
self.win = ListFrame(None, -1, "")
self.win.Show(true)

however, this seems clumsy (I have to maintain a reference to every window that uses a wxImageList) and it took me quite some time to figure it out.

# ========== code start =================

from wxPython.wx import *

class ListFrame(wxFrame):
def __init__(self, parent, id, title):
wxFrame.__init__(self, parent, id, title)
list = wxListCtrl(self, -1, style = wxLC_REPORT)
list.InsertColumn(0, "col 0")
self.il = wxImageList(16, 16)
list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
icon = wxBitmap('smiles.bmp', wxBITMAP_TYPE_BMP)
index = self.il.Add(icon)
for i in range(10):
list.InsertImageStringItem(0, 'label %d' % i, index)

class Application(wxApp):
def OnInit(self):
win = ListFrame(None, -1, "")
win.Show(true)
return true

app = Application()
app.MainLoop()

"Kevin A. White" wrote:

···

Sorry for the repost, but I am down to the last part of my app, and no one
commented on why my code is giving me grief. I am trying to use the
wxLC_ICON style with the wxListCtrl, but the icons never display. Below is a
test code snippet. Anyone see what I am doing wrong?

from wxPython.wx import *

class CTest( wxFrame ):
   def __init__(self):
      wxFrame.__init__(self, NULL, -1, "test" )
      il = wxImageList(16, 16)
      idx1 = il.Add( wxBitmap('inboxs.bmp', wxBITMAP_TYPE_BMP) )
      list = wxListCtrl(self, -1, style = wxLC_ICON )
      list.SetImageList(il, wxIMAGE_LIST_NORMAL)
      list.InsertImageStringItem(0, "test label", idx1)

class CApp( wxApp ):
   def OnInit(self):
      frame = CTest()
      frame.Show(true)
      self.SetTopWindow(frame)
      return true

app = CApp(0)
app.MainLoop()

_______________________________________________
wxPython-users mailing list wxPython-users@wxwindows.org
http://wxwindows.org/mailman/listinfo/wxpython-users

Robin Dunn discussed an issue with wxImageList (Dec 23, 1999) that
causes the images to get garbage-collected prematurely. He proposed a
work-around in maintaining a reference to the wxImageList to prevent it
from getting garbage-collected. However, not only is it necessary to
maintain a reference to the list itself, but also to the window that
contains the list.

This isn't quite true. If the window has had any events hooked with EVT_*
functions then a reference is automatically saved to the window in its event
table and you won't have to save your own reference unless you need to
access it yourself later. If there are no events hooked then there are no
extra references to the Python object and so it will get cleaned up like any
other Python object.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

access it yourself later. If there are no events hooked then there are no
extra references to the Python object and so it will get cleaned up like

any

other Python object.

I should probably add that the C++ object for the window will get deleted
when its parent window is deleted. If it has no parent then it is deleted
when it is closed (frames) or when its Destroy method is called (frames and
dialogs).

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!