如何在 wxPython 对象列表视图中更改现有项字符串的值list_ctrl



我知道我可以像这样使用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)方法,但它设置了由控件显示的modelObjects列表,这不是我想要做的。

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

您可以通过设置ObjectListView实例的cellEditMode来打开单元格的"可编辑性"。这样就可以了:

self.list_ctrl.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK

ObjectListView文档在这里讨论了这个:

  • http://objectlistview.sourceforge.net/python/cellEditing.html(旧文档)
  • http://objectlistview-python-edition.readthedocs.org/en/latest/cellEditing.html(新文件)

我在下面的教程中也提到了它:

  • http://www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/

UPDATE:遍历对象并以编程方式更改它们有点棘手。幸运的是,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()

相关内容

  • 没有找到相关文章

最新更新