3

我知道我可以list_ctrl.GetStringValueAt()像这样使用来获取特定对象的值ObjectListView

class myOLV(ObjectListView):
    ... code to handle specific events

class myPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.list_ctrl = myOLV(self, wx.ID_ANY, style=wx.LC_REPORT)
        self.setColumns()

        # . . .

        self.submit_button = wx.Button(self, label="Submit")
        # ... add to sizer here, skipping tedious stuff in this example
        self.submit_button.Bind(wx.EVT_BUTTON, self.onSubmit)

    def setColumns(self):
        self.list_ctrl.SetColumns([
            ColumnDefn("Column 1", "left", 128, "col1"),
            ColumnDefn("Column 2", "left", 128, "col2"),
            ColumnDefn("Column 3", "left", 128, "col3")
        )]

    def onSubmit(self, event):
        objects = self.list_ctrl.GetObjects()
        for obj in objects:
            col1 = self.list_ctrl.GetStringValueAt(obj, 1)
            # how could I change the above value here?
            col2 = self.list_ctrl.GetStringValueAt(obj, 2)
            col3 = self.list_ctrl.GetStringValueAt(obj, 3)

但是,如果我想在当前循环和列索引中更改给定 obj 的字符串的值怎么办?文档似乎没有将当前对象和列索引作为参数的方法,那么我该怎么做(不删除和重新填充元素)?

我从文档中看到有一种SetValue(modelObjects, preserveSelection=False)方法,但它设置了要由控件显示的模型对象列表,这不是我想要做的。

在这种情况下是否有SetStringValueAt(modelObject, columnIndex)方法或解决方法可以做同样的事情?

4

1 回答 1

4

您可以通过设置 ObjectListView 实例的cellEditMode. 像这样的东西会起作用:

self.list_ctrl.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK

ObjectListView 文档在这里讨论了这一点:

我还在以下教程中提到它:

更新:迭代对象并以编程方式更改它们有点棘手。幸运的是,ObjectListView 提供了RefreshObject,它允许我们更改特定对象然后刷新它。这是一个示例脚本:

# OLVcheckboxes.py

import wx
from ObjectListView import ObjectListView, ColumnDefn

########################################################################
class Results(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, tin, zip_code, plus4, name, address):
        """Constructor"""
        self.tin = tin
        self.zip_code = zip_code
        self.plus4 = plus4
        self.name = name
        self.address = address


########################################################################
class ProvPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.columns = {0: 'tin',
                        1: 'zip_code',
                        2: 'plus4',
                        3: 'name',
                        4: 'address'}

        self.test_data = [Results("123456789", "50158", "0065", "Patti Jones",
                                  "111 Centennial Drive"),
                          Results("978561236", "90056", "7890", "Brian Wilson",
                                  "555 Torque Maui"),
                          Results("456897852", "70014", "6545", "Mike Love",
                                  "304 Cali Bvld")
                          ]
        self.resultsOlv = ObjectListView(self, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        self.resultsOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK

        self.setResults()

        self.column_cbo = wx.ComboBox(self, value='tin',
                                      choices=self.columns.values())
        modify_btn = wx.Button(self, label='Modify Column 1 Cells')
        modify_btn.Bind(wx.EVT_BUTTON, self.onModify)

        mainSizer.Add(self.resultsOlv, 1, wx.EXPAND|wx.ALL, 5)
        mainSizer.Add(self.column_cbo, 0, wx.CENTER|wx.ALL, 5)
        mainSizer.Add(modify_btn, 0, wx.CENTER|wx.ALL, 5)
        self.SetSizer(mainSizer)

    #----------------------------------------------------------------------
    def onModify(self, event):
        """
        Modify cells
        """
        objects = self.resultsOlv.GetObjects()
        column = self.column_cbo.GetValue()
        for obj in objects:
            value = 'Row #%s' % self.resultsOlv.GetIndexOf(obj)
            setattr(obj, column, value)
            self.resultsOlv.RefreshObject(obj)

    #----------------------------------------------------------------------
    def setResults(self):
        """"""
        self.resultsOlv.SetColumns([
            ColumnDefn("TIN", "left", 100, "tin"),
            ColumnDefn("Zip", "left", 75, "zip_code"),
            ColumnDefn("+4", "left", 50, "plus4"),
            ColumnDefn("Name", "left", 150, "name"),
            ColumnDefn("Address", "left", 200, "address")
        ])
        self.resultsOlv.CreateCheckStateColumn()
        self.resultsOlv.SetObjects(self.test_data)


########################################################################
class ProvFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        title = "OLV Checkbox Tutorial"
        wx.Frame.__init__(self, parent=None, title=title, size=(1024, 768))
        panel = ProvPanel(self)


#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = ProvFrame()
    frame.Show()
    app.MainLoop()
于 2015-08-26T18:04:55.003 回答