Search code examples
pythonwxpythonwxwidgets

Problems with wx.ListCtrl


Here is a snippet

    self.list_ctrl = wx.ListCtrl(self, size=(-1,100),
                     style=wx.LC_ICON|wx.LC_ALIGN_LEFT
                     )
    il = wx.ImageList(16,16,True)
    png = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN,wx.ART_OTHER, (16,16))
    il.Add(png)
    self.list_ctrl.AssignImageList(il,wx.IMAGE_LIST_NORMAL)


    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
    self.SetSizer(sizer)

    self.list_ctrl.InsertImageStringItem(0,"1",0)
    self.list_ctrl.InsertImageStringItem(1,"2",0)

My problem is that the icons appear to the top of the text which should not happen because I put wx.LC_ALIGN_LEFT in the style. I would like the icons to appear left of the text.

Another problem is that, I want one element per row. In my code it is almost like one element per column.

Can anyone help me with any of these problems? Thanks.


Solution

  • Looking at the wxPython demo for the ListCtrl, it looks like they use SetImageList() instead of AssignImageList(). Not sure what the difference is though. I don't see where you're inserting any text though. You'd need to use SetStringItem to put text in the other columns from what I can see.

    EDIT: Code from wxPython Demo package, ListCtrl demo:

    self.il = wx.ImageList(16, 16)
    
    self.idx1 = self.il.Add(images.Smiles.GetBitmap())
    self.sm_up = self.il.Add(images.SmallUpArrow.GetBitmap())
    self.sm_dn = self.il.Add(images.SmallDnArrow.GetBitmap())
    

    And then we add data / images to the widget

    def PopulateList(self):
        if 0:
            # for normal, simple columns, you can add them like this:
            self.list.InsertColumn(0, "Artist")
            self.list.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT)
            self.list.InsertColumn(2, "Genre")
        else:
            # but since we want images on the column header we have to do it the hard way:
            info = wx.ListItem()
            info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
            info.m_image = -1
            info.m_format = 0
            info.m_text = "Artist"
            self.list.InsertColumnInfo(0, info)
    
            info.m_format = wx.LIST_FORMAT_RIGHT
            info.m_text = "Title"
            self.list.InsertColumnInfo(1, info)
    
            info.m_format = 0
            info.m_text = "Genre"
            self.list.InsertColumnInfo(2, info)
    
        items = musicdata.items()
        for key, data in items:
            index = self.list.InsertImageStringItem(sys.maxint, data[0], self.idx1)
            self.list.SetStringItem(index, 1, data[1])
            self.list.SetStringItem(index, 2, data[2])
            self.list.SetItemData(index, key)