listcrlhack:在ListC中设置ListItems文本

2024-05-14 06:55:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我使用这个函数:_列表项.SetText(“Blah”)但文本不变?我也尝试过SetItemText()函数,但也不起作用。在

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

  • listcrl设置了LC\u报表样式
  • 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"

Tags: rectselfeventondeftypecelledit
1条回答
网友
1楼 · 发布于 2024-05-14 06:55:23

当你使用_列表项.SetText(“Blah”)您还需要使用类似于self.SetItem(列表项)。或者您可以使用listcrl的SetItemText方法,而不必处理wx.ListItem完全反对。在

相关问题 更多 >

    热门问题