您可以通过设置 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()