python-3.6listctrlwxpython

WxPython select item


I want get value from selected item in list when I click a button, and post it in another list.

    self.list_ctrl = wx.ListCtrl(panel, size=(30,100), pos=(20,30),
                     style=wx.LC_REPORT
                     |wx.BORDER_SUNKEN
                     )

    self.list_ctrl2 = wx.ListCtrl(panel, size=(200,300),pos=(60,200), style=wx.LC_REPORT|wx.BORDER_SUNKEN)




    btn = wx.Button(panel, label="Add Line")
    btn2 = wx.Button(panel, label="Get Column 0")
    btn3 = wx.Button(panel, label="select")


    btn.Bind(wx.EVT_BUTTON, self.add_line)
    btn2.Bind(wx.EVT_BUTTON, self.getColumn)
    btn3.Bind(wx.EVT_BUTTON, self.getSelection)


def add_line(self, event):
    line = "Line %s" % self.index
    self.list_ctrl.InsertItem(self.index, line)
    self.list_ctrl.SetItem(self.index, 1, "01/19/2010")
    self.list_ctrl.SetItem(self.index, 2, "USA")
    self.index += 1

def getColumn(self, event):

    item = self.list_ctrl.GetItem(itemIdx=0, col=0)
    print (item.GetText())
    self.list_ctrl2.InsertItem(item)

def getSelection (self, event):
    item2=self.list_ctrl.GetNextSelected()
    self.list_ctrl2.InsertItem(item2)

I have try with GetItem, GetNextSelected. I have search on https://docs.wxpython.org/wx.ListCtrl.html#wx.ListCtrl.GetNextSelected But I think I have a syntaxic problem and I didn't find


Solution

  • I have found my answer

    define list :

    self.userList = wx.ListCtrl(self.panel,pos=(50,200), size=(-1,500),style=wx.LC_REPORT)
    

    Bind event :

    self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.addSendList, self.userList)
    

    define event :

    def AddSendList(self, evt):
        choice= self.userList.GetFirstSelected()
        item = self.userList.GetItem(itemIdx=choice, col=0)
        textItem=item.GetText()
        print(choice)
        print(textItem)
        self.sendList.InsertItem(0,textItem)
    

    With this Function I can have the selected value in a ListCtrl, and put value in a other ListCtrl.