WxPython ListCtrl索引0项不可编辑

时间:2016-03-09 12:12:47

标签: wxpython listctrl

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' index"参数"作为不可编辑的listCtrl和' 1'指数'价值"作为可编辑的listCtrl。

2 个答案:

答案 0 :(得分:0)

您可以使用

中的EditableListCtrl执行此操作
import wx.lib.mixins.listctrl as listmix

中为混音创建一个类
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中列的编号

答案 1 :(得分:0)

使用wx.ListCtrl几乎总是很痛苦。我建议您尝试使用ObjectListView代替

General usage

Cell editing

实施例:

#!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()