ListCtrl Hack:在ListCtrl中设置ListItems文本

时间:2010-12-26 03:14:23

标签: python wxpython

我正在尝试在ListCtrl中设置项目的文本内容。问题是它不起作用。

我使用此函数:_listItem.SetText(“Blah”)但文本没有改变?我也尝试过函数SetItemText(),但这也不起作用。

一些重要信息(可能有帮助):

  • ListCtrl具有LC_REPORT样式集
  • ListCtrl有多列
  • 我有自己的类,它是ListCtrl ...它是ListCtrl对象和wx.lib.mixins.TextEditMixins对象的子类(所以我可以编辑每个项目/单元格)

我的代码:

class AddStockListCtrl( ListCtrl, listmix.TextEditMixin ):
    """ """

    # Class Variables:

    # self.parent
    # self.type_cell
    # self.type_cb


    # Class Functions:

    def __init__( self, _parent ):
        """ Constructor """

        ListCtrl.__init__( self, parent=_parent, id=wx.NewId(), style=wx.LC_EDIT_LABELS|wx.LC_REPORT )

        AddStockListCtrl.def_data = ('ABC', "Registered",
                                     '0.00', '500')  # wx.lib.masked.NumCtrl( self, value="0.00" ), wx.lib.masked.NumCtrl( self, value="0.00" ) )
        listmix.TextEditMixin.__init__(self)

        self.parent    = _parent
        self.type_cb   = wx.ComboBox( self, choices=('Registered', 'Tracking'))
        self.type_cell = None

        self.InsertColumn( 0, heading="Code", width=40 )
        self.InsertColumn( 1, heading="Type", width=50 )
        self.InsertColumn( 2, heading="Purchase Price", width=100 )
        self.InsertColumn( 3, heading="Purchase Quantity", width=110 )
        self.type_cb.SetSelection(0)
        self.type_cb.Hide()

        self.add_stock_row()

        self.Bind( wx.EVT_LIST_END_LABEL_EDIT,   self.on_end_edit )  # .on_validate_value )
        self.Bind( wx.EVT_LIST_BEGIN_LABEL_EDIT, self.on_begin_edit )


    def on_begin_edit( self, event ):
        """ Post: """

        if event.GetItem().GetColumn() == 1:

            self.type_cell = event.GetItem().GetId()
            item           = self.type_cell
            rect= self.GetItemRect( event.GetItem().GetId() )
            rect.SetLeft ( self.GetColumnWidth(0)+2 )
            rect.SetWidth( self.GetColumnWidth(1)-2 )

            @AfterEx( rect )
            def postedit( rect ):
                self.type_cb.SetRect( rect )
                self.type_cb.SetFocus()
                self.type_cb.Show()
                self.type_cb.Raise()
                #event.Veto()


    def on_end_edit( self, event ):
        """ Post: """

        if self.type_cell != -1:
            sel_type = str(self.type_cb.GetValue())
            print "Doing end edit & type_cell != None"
            print self.GetItemCount()
            print sel_type

            if sel_type == "Registered":

                for row in range(self.GetItemCount()):

                    #self.GetItem(row,2).SetEditable(True) # by the way is there a way to make a ListItem not editable?
                    #self.GetItem(row,3).SetEditable(True)
                    pass

            else: # sel_type == "Tracking"

                for row in range(self.GetItemCount()):

                    p_price = self.GetItem(row,2)
                    p_quant = self.GetItem(row,3)
                    p_price.SetText("0.00")
                    p_quant.SetText("500")
                    #p_price.SetEditable(False)
                    #p_quant.SetEditable(False)

            self.type_cb.Hide()
            #event.GetItem().SetText( sel_type )
            self.SetItemText( self.type_cell, sel_type ) # HERE the text should change but doesn't?!
            event.Veto()
        else:
            print "It == None"

1 个答案:

答案 0 :(得分:2)

当你使用_listItem.SetText(“Blah”)时,你还需要将项目放回listctrl中,类似self.SetItem(_listItem)。或者你可以使用listctrl的SetItemText方法,而不必处理wx.ListItem对象。