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