Adding items to wx.ListCtrl?

Hi all,
Another question for everyone. My app was working well with wx.ListBox, but then I ran across wx.ListCtrl and thought it would fit my needs far better. However, I can’t get it to work right, and I think it’s because I’m misunderstanding something about inserting data. The great thing about ListBox is that all you give it is a list of strings, and it does the rest.

There are two methods for adding a new row, for some reason. There’s AddStringItem(rowIndex, string) and InsertStringItem(rowIndex, columnIndex, string). Why are there two, and what does the first one do that the second doesn’t? Do I need to use both, or could I just use the InsertStringItem method for everything? Thanks.

···

Alex Hall

ahall@autodist.com

I almost always use InsertStringItem. I can’t think of a time when I used the other one. If you need some ideas, you might find this article helpful:

Personally, I like ObjectListView better, but that might be because it just fits my brain better.

Mike

···

On Thursday, January 28, 2016 at 7:55:54 PM UTC-6, Alex Hall wrote:

Hi all,
Another question for everyone. My app was working well with wx.ListBox, but then I ran across wx.ListCtrl and thought it would fit my needs far better. However, I can’t get it to work right, and I think it’s because I’m misunderstanding something about inserting data. The great thing about ListBox is that all you give it is a list of strings, and it does the rest.

There are two methods for adding a new row, for some reason. There’s AddStringItem(rowIndex, string) and InsertStringItem(rowIndex, columnIndex, string). Why are there two, and what does the first one do that the second doesn’t? Do I need to use both, or could I just use the InsertStringItem method for everything? Thanks.

Alex Hall

ah...@autodist.com

Sorry I'm not replying in-line, I'm on the Gmail website.

My mistake. I said "add" when I meant "set. You have SetStringItem,
and InsertStringItem, and I'm not sure why there needs to be both.

The article you linked to is the same one I've been using for
reference the last day--extremely helpful! There, you have an example
of both methods being used. Quoting it:

        line = "Line %s" % self.index
        self.list_ctrl.InsertStringItem(self.index, line) #AH: insert
        self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010") #AH:
set, not insert
        self.list_ctrl.SetStringItem(self.index, 2, "USA")
        self.index += 1

My listCtrl is working properly (the problem was my indexing of the
arrays my constructor was taking in). Still, I'd like to understand
why WX uses both of the afore mentioned methods instead of using
SetStringItem all the time. Is Insert just a convenience method,
automatically using a column index of 0?

···

On 1/29/16, Mike Driscoll <kyosohma@gmail.com> wrote:

On Thursday, January 28, 2016 at 7:55:54 PM UTC-6, Alex Hall wrote:

Hi all,
Another question for everyone. My app was working well with wx.ListBox,
but then I ran across wx.ListCtrl and thought it would fit my needs far
better. However, I can't get it to work right, and I think it's because
I'm
misunderstanding something about inserting data. The great thing about
ListBox is that all you give it is a list of strings, and it does the
rest.

There are two methods for adding a new row, for some reason. There's
AddStringItem(rowIndex, string) and InsertStringItem(rowIndex,
columnIndex,
string). Why are there two, and what does the first one do that the second

doesn't? Do I need to use both, or could I just use the InsertStringItem
method for everything? Thanks.

--
Alex Hall
ah...@autodist.com <javascript:>

I almost always use InsertStringItem. I can't think of a time when I used
the other one. If you need some ideas, you might find this article helpful:

wxPython: wx.ListCtrl Tips and Tricks - Mouse Vs Python

Personally, I like ObjectListView better, but that might be because it just

fits my brain better.

Mike

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sorry I’m not replying in-line, I’m on the Gmail website.

My mistake. I said “add” when I meant "set. You have SetStringItem,

and InsertStringItem, and I’m not sure why there needs to be both.

Frankly I’ve never been a fan of this design decision. But it does appear that you need both

The article you linked to is the same one I’ve been using for

reference the last day–extremely helpful! There, you have an example

of both methods being used. Quoting it:

    line = "Line %s" % self.index

    self.list_ctrl.InsertStringItem(self.index, line) #AH: insert

    self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010") #AH:

set, not insert

    self.list_ctrl.SetStringItem(self.index, 2, "USA")

    self.index += 1

My listCtrl is working properly (the problem was my indexing of the

arrays my constructor was taking in). Still, I’d like to understand

why WX uses both of the afore mentioned methods instead of using

SetStringItem all the time. Is Insert just a convenience method,

automatically using a column index of 0?

Basically you use InsertStringItem to insert a row. Then you use SetStringItem to add text to the various columns in the row that you just added. If you’re using a newer wxPython (i.e. 2.9 or above),then you could use the DVC_ListCtrl which is a bit simpler in its API. I should really write an article about that one and convert the examples from the original over. Just for fun, I did take my original example that you mentioned and converted it:

import wx
import wx.dataview as dv

···

On Friday, January 29, 2016 at 10:34:11 AM UTC-6, Alex Hall wrote:

########################################################################
class MyPanel(wx.Panel):
“”“”“”

#----------------------------------------------------------------------
def __init__(self, parent):
    """Constructor"""
    super(MyPanel, self).__init__(parent)
    self.index = 0
    self.dvlc = dvlc = dv.DataViewListCtrl(self)
   
    self.dvlc.AppendTextColumn('Subject')
    self.dvlc.AppendTextColumn('Due')
    self.dvlc.AppendTextColumn('Location')
   
    btn = wx.Button(self, label="Add Line")
    btn.Bind(wx.EVT_BUTTON, self.add_line)
   
    main_sizer = wx.BoxSizer(wx.VERTICAL)
    main_sizer.Add(self.dvlc, 1, wx.EXPAND)
    main_sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
    self.SetSizer(main_sizer)
   
#----------------------------------------------------------------------
def add_line(self, event):
    line = "Line %s" % self.index
    item = (line, "01/19/2010", 'USA')
    self.dvlc.AppendItem(item)
    self.index += 1

########################################################################
class MyFrame(wx.Frame):
“”“”“”

#----------------------------------------------------------------------
def __init__(self, *args, **kwargs):
    """Constructor"""
    super(MyFrame, self).__init__(*args, **kwargs)
    panel = MyPanel(self)
    self.Show()

if name == ‘main’:
app = wx.App(False)
frame = MyFrame(None, title=‘Test’)
app.MainLoop()

Hopefully that helps you out a bit.
Mike