wx.ListCtrl get item select from a button.

Hi,

I’ve a wx.ListCtrl like

class MyListCtrl(wx.ListCtrl):

     def __init__(self, parent, dataSource):
    wx.ListCtrl.__init__(self, parent,
        style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_VIRTUAL)

    .........

with this

         self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)

and

           def OnItemSelected(self, evt):
   
                self.item = evt.GetItem()
                print "Item selected:", self.item.GetText()
                strSelected =item.GetText()
                wx.MessageBox(strSelected,"Item selected:", wx.OK)
   
                return self.item

How I can call the def OnItemSelected from a button in the frame?

I try so

class MyFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1,
“Virtual wx.ListCtrl”,
size=(600,400))
panel = wx.Panel(self)

    vbox = wx.BoxSizer(wx.VERTICAL)

    .....................

     self.list = MyListCtrl(panel, DataSource())

               

    self.cmdEdit = wx.Button(panel, -1, '&Edit', size=(70, 30))
    self.Bind(wx.EVT_BUTTON, self.fctEdit, self.cmdEdit)

      def fctEdit(self,event): 
            id = self.list.OnItemSelected(self)

            print id

Python return this

AttributeError: ‘MyFrame’ object has no attribute ‘GetItem’

What is wrong?

Beppe

Giuseppe Costanzi wrote:

          def fctEdit(self,event): id = self.list.OnItemSelected(self)
                print id
Python return this
AttributeError: 'MyFrame' object has no attribute 'GetItem'
What is wrong?

You are passing the frame for the evt parameter of the OnItemSelected method, but it is expecting a wx.ListEvent. A better way to approach this is to refactor the OnItemSelected functionality to separate out the parts that are event specific, and put the rest into another method. Then you can call that other method from the event handler, and also call it from other parts of the program passing it the item to work on.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!