WxPython ListCtrl 索引 0 项不可编辑
WxPython ListCtrl index 0 item non editable
self.testParameters = self.testParameterListCtrl(self, -1, style=wx.LC_REPORT |wx.LC_HRULES | wx.LC_VRULES | wx.LC_SINGLE_SEL)
self.testParameters.InsertColumn(0, "Parameter", wx.LIST_FORMAT_CENTER, -1)
self.testParameters.InsertColumn(1, "Value", wx.LIST_FORMAT_CENTER, -1)
self.testParameters.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.onModifyTestParameter )
for parameter, value in self.keywordArgs.iteritems():
self.testParameters.InsertStringItem(index, parameter, 0)
self.testParameters.SetStringItem(index, 1, str(value))
if parameter in self.workloadTests.values():
self.testParameters.SetStringItem(index, 1, self.testWorkload.GetValue())
# Cell Colors
if (index % 2):
self.testParameters.SetItemBackgroundColour(index, self.listBGColor_1)
else:
self.testParameters.SetItemBackgroundColour(index, self.listBGColor_2)
index += 1
在此代码中,我想禁用“0”索引 "parameter" 作为不可编辑的 listCtrl 和“1”索引“值”作为可编辑的 listCtrl。
您可以使用
中的 EditableListCtrl 来执行此操作
import wx.lib.mixins.listctrl as listmix
为
中的混音创建 class
class EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin):
# TextEditMixin allows any column to be edited other than those specifically vetoed
def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
"""Constructor"""
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
listmix.TextEditMixin.__init__(self)
将您的 listctrl 定义为可编辑
self.listCtrl = EditableListCtrl(my_panel1, -1, style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_VRULES, size=(1145,285))
然后绑定某些项目的否决权:self.listCtrl.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnVetoItems)
定义你的否决权
def OnVetoItems(self, event):
if event.m_itemIndex == 0:
event.Veto()
return
注意也可以使用 event.m_col
并检查 listctrl
中的列号来否决列
使用 wx.ListCtrl
s 几乎总是一件痛苦的事。我建议您尝试使用 ObjectListView
而不是
示例:
#!python3
import wx
from ObjectListView import ObjectListView, ColumnDefn
random_objects = [
{"parameter": "Test", "value": "1"},
{"parameter": "Test2", "value": "2"}
]
class MainFrame(wx.Frame):
def __init__(self):
super(MainFrame, self).__init__(parent=None, title="OLV Test")
panel = MainPanel(self)
self.Show()
class MainPanel(wx.Panel):
def __init__(self, parent):
super(MainPanel, self).__init__(parent)
self.olv = ObjectListView(parent=self, style=wx.LC_REPORT | wx.BORDER_SUNKEN, sortable=True,
useAlternateBackColors=False, cellEditMode=ObjectListView.CELLEDIT_DOUBLECLICK)
self.olv.SetColumns([
ColumnDefn(title="Parameter", valueGetter="parameter", isSpaceFilling=True, isEditable=False),
ColumnDefn(title="Value", valueGetter="value", isSpaceFilling=True)
])
get_button = wx.Button(self, label="Print objects")
top_sizer = wx.BoxSizer(wx.VERTICAL) # Root sizer
olv_sizer = wx.BoxSizer(wx.VERTICAL) # For the ObjectListView
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
olv_sizer.Add(self.olv, 1, wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ALIGN_TOP, 5)
button_sizer.Add(get_button, 0, wx.ALL | wx.ALIGN_LEFT, 5)
top_sizer.Add(olv_sizer, 1, wx.ALL | wx.EXPAND, 5)
top_sizer.Add(button_sizer, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizerAndFit(top_sizer)
self.Bind(wx.EVT_BUTTON, handler=self.get_objects, source=get_button)
self.olv.SetObjects(random_objects)
def get_objects(self, event):
for item in self.olv.GetObjects():
print("Parameter: {param}".format(param=item['parameter']))
print("Value: {val}".format(val=item['value']))
if __name__ == "__main__":
wx_app = wx.App()
frame = MainFrame()
wx_app.MainLoop()
当用户编辑列表时,random_objects
中的值也会更新,因此如果您愿意,您实际上可以使用它来代替 self.olv.GetObjects()
。
self.testParameters = self.testParameterListCtrl(self, -1, style=wx.LC_REPORT |wx.LC_HRULES | wx.LC_VRULES | wx.LC_SINGLE_SEL)
self.testParameters.InsertColumn(0, "Parameter", wx.LIST_FORMAT_CENTER, -1)
self.testParameters.InsertColumn(1, "Value", wx.LIST_FORMAT_CENTER, -1)
self.testParameters.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.onModifyTestParameter )
for parameter, value in self.keywordArgs.iteritems():
self.testParameters.InsertStringItem(index, parameter, 0)
self.testParameters.SetStringItem(index, 1, str(value))
if parameter in self.workloadTests.values():
self.testParameters.SetStringItem(index, 1, self.testWorkload.GetValue())
# Cell Colors
if (index % 2):
self.testParameters.SetItemBackgroundColour(index, self.listBGColor_1)
else:
self.testParameters.SetItemBackgroundColour(index, self.listBGColor_2)
index += 1
在此代码中,我想禁用“0”索引 "parameter" 作为不可编辑的 listCtrl 和“1”索引“值”作为可编辑的 listCtrl。
您可以使用
中的 EditableListCtrl 来执行此操作import wx.lib.mixins.listctrl as listmix
为
中的混音创建 classclass EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin):
# TextEditMixin allows any column to be edited other than those specifically vetoed
def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
"""Constructor"""
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
listmix.TextEditMixin.__init__(self)
将您的 listctrl 定义为可编辑
self.listCtrl = EditableListCtrl(my_panel1, -1, style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_VRULES, size=(1145,285))
然后绑定某些项目的否决权:self.listCtrl.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnVetoItems)
定义你的否决权
def OnVetoItems(self, event):
if event.m_itemIndex == 0:
event.Veto()
return
注意也可以使用 event.m_col
并检查 listctrl
使用 wx.ListCtrl
s 几乎总是一件痛苦的事。我建议您尝试使用 ObjectListView
而不是
示例:
#!python3
import wx
from ObjectListView import ObjectListView, ColumnDefn
random_objects = [
{"parameter": "Test", "value": "1"},
{"parameter": "Test2", "value": "2"}
]
class MainFrame(wx.Frame):
def __init__(self):
super(MainFrame, self).__init__(parent=None, title="OLV Test")
panel = MainPanel(self)
self.Show()
class MainPanel(wx.Panel):
def __init__(self, parent):
super(MainPanel, self).__init__(parent)
self.olv = ObjectListView(parent=self, style=wx.LC_REPORT | wx.BORDER_SUNKEN, sortable=True,
useAlternateBackColors=False, cellEditMode=ObjectListView.CELLEDIT_DOUBLECLICK)
self.olv.SetColumns([
ColumnDefn(title="Parameter", valueGetter="parameter", isSpaceFilling=True, isEditable=False),
ColumnDefn(title="Value", valueGetter="value", isSpaceFilling=True)
])
get_button = wx.Button(self, label="Print objects")
top_sizer = wx.BoxSizer(wx.VERTICAL) # Root sizer
olv_sizer = wx.BoxSizer(wx.VERTICAL) # For the ObjectListView
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
olv_sizer.Add(self.olv, 1, wx.LEFT | wx.RIGHT | wx.EXPAND | wx.ALIGN_TOP, 5)
button_sizer.Add(get_button, 0, wx.ALL | wx.ALIGN_LEFT, 5)
top_sizer.Add(olv_sizer, 1, wx.ALL | wx.EXPAND, 5)
top_sizer.Add(button_sizer, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizerAndFit(top_sizer)
self.Bind(wx.EVT_BUTTON, handler=self.get_objects, source=get_button)
self.olv.SetObjects(random_objects)
def get_objects(self, event):
for item in self.olv.GetObjects():
print("Parameter: {param}".format(param=item['parameter']))
print("Value: {val}".format(val=item['value']))
if __name__ == "__main__":
wx_app = wx.App()
frame = MainFrame()
wx_app.MainLoop()
当用户编辑列表时,random_objects
中的值也会更新,因此如果您愿意,您实际上可以使用它来代替 self.olv.GetObjects()
。