wxListCtrl and EVT_LIST_ITEM_SELECTED

I'm trying to get a list control to respond to the the item selected event.
This is the macro I'm using. I'm not sure why it doesn't work

EVT_LIST_ITEM_SELECTED(self.list_playlists, XRCID("LIST_PLAYLISTS"), self.OnItemSelected)

It should call OnItemSelected, defined as follows.

def OnItemSelected(self, event):
        print "OnItemSekected"

I've checked that XRCID(""LIST_PLAYLISTS) == self.list_playlists.GetId()

What could be the reasons for it not responding to the event? Are there some flags that must be passed to the list control on creation?

···

--
sashan
http://www.cs.auckland.ac.nz/~sgov008

I've attached my source code for the init function of the dialog class I'm trygin to create. I can't see where I'm going wrong. The events don't work (i.e. OnItemSelected and OnItemActivated aren't called.

class DlgPlaylists(wxDialogPtr):
  def __init__(self, parent, db_cursor):
    print "init"
    res = wxXmlResource("./dialog_playlists.xrc")
    dlg = res.LoadDialog(parent, "DLG_PLAYLISTS")
    wxDialogPtr.__init__(self, dlg.this)
    self.db_cursor = db_cursor
    self.list_playlists = XRCCTRL(self, "LIST_PLAYLISTS")
    ##Query the database for all the playlists
    self.db_cursor.execute("select * from playlists;")
    playlists = self.db_cursor.fetchall()
    for playlist, _ in playlists:
      self.list_playlists.Append(playlist)

    EVT_LIST_ITEM_SELECTED(self.list_playlists, XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
    EVT_LIST_ITEM_ACTIVATED(self.list_playlists, XRCID("LIST_PLAYLISTS"), self.OnItemActivated)
    
  def OnItemSelected(self, event):
    print "OnItemSelected"

  def OnItemActivated(self,event):
    print "OnItemActivated"

Hi Sashan,

sashan wrote:

[snip]
> EVT_LIST_ITEM_SELECTED(self.list_playlists,
> XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
> EVT_LIST_ITEM_ACTIVATED(self.list_playlists,
> XRCID("LIST_PLAYLISTS"), self.OnItemActivated)
>
Try this:
EVT_LIST_ITEM_SELECTED(self,
      XRCID("LIST_PLAYLISTS"), self.OnItemSelected)

The way I understand the events is that the first parameter is
object which contains the can handle the event not the object which
emits the event.
The second paramter is the wxWindows ID of the objects which emits
the event.

Hope that helps
     Adi

Adi Sieker wrote:

Hi Sashan,

sashan wrote:

[snip]
> EVT_LIST_ITEM_SELECTED(self.list_playlists,
> XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
> EVT_LIST_ITEM_ACTIVATED(self.list_playlists,
> XRCID("LIST_PLAYLISTS"), self.OnItemActivated)
>
Try this:
EVT_LIST_ITEM_SELECTED(self,
     XRCID("LIST_PLAYLISTS"), self.OnItemSelected)

The way I understand the events is that the first parameter is
object which contains the can handle the event not the object which
emits the event.
The second paramter is the wxWindows ID of the objects which emits
the event.

I tried the way you suggested but it didn't make a difference. I had a look at how boa-constructor handled list boxes and the 1st parameter is the control that generates the event. For example:
EVT_LIST_ITEM_SELECTED(self.listCtrl1, wxID_WXDIALOG1LISTCTRL1,
          self.OnListctrl1ListItemSelected)

I'm sure the Id / /am passing to/ /the event handler is right because self.list_playlist.GetId() == XRCID("LIST_PLAYLISTS")

sashan wrote:

Adi Sieker wrote:

Hi Sashan,

sashan wrote:

[snip]
> EVT_LIST_ITEM_SELECTED(self.list_playlists,
> XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
> EVT_LIST_ITEM_ACTIVATED(self.list_playlists,
> XRCID("LIST_PLAYLISTS"), self.OnItemActivated)
>
Try this:
EVT_LIST_ITEM_SELECTED(self,
     XRCID("LIST_PLAYLISTS"), self.OnItemSelected)

The way I understand the events is that the first parameter is
object which contains the can handle the event not the object which
emits the event.
The second paramter is the wxWindows ID of the objects which emits
the event.

I tried the way you suggested but it didn't make a difference. I had a look at how boa-constructor handled list boxes and the 1st parameter is the control that generates the event. For example:
EVT_LIST_ITEM_SELECTED(self.listCtrl1, wxID_WXDIALOG1LISTCTRL1,
              self.OnListctrl1ListItemSelected)

I'm sure the Id / /am passing to/ /the event handler is right because self.list_playlist.GetId() == XRCID("LIST_PLAYLISTS")

Also I think the way you suggest should also work since the example over here http://wiki.wxpython.org/index.cgi/UsingXmlResources uses that method for the event handlers for the button.

The worst thing about this problem is that if I add an event handler for a button that is a child of the dialog it works! This is really puzzling.....I can't see what the difference is between the button event handler and list box event handler.

class DlgPlaylists(wxDialogPtr):
    def __init__(self, parent, db_cursor):
        print "init"
        res = wxXmlResource("./dialog_playlists.xrc")
        dlg = res.LoadDialog(parent, "DLG_PLAYLISTS") wxDialogPtr.__init__(self, dlg.this)
        self.db_cursor = db_cursor
        self.list_playlists = XRCCTRL(self, "LIST_PLAYLISTS")
        self.button_ok = XRCCTRL(self, "BUTTON_OK")
        ##Query the database for all the playlists
        self.db_cursor.execute("select * from playlists;")
        playlists = self.db_cursor.fetchall() for playlist, _ in playlists:
            self.list_playlists.Append(playlist)

        EVT_LIST_ITEM_SELECTED(self.list_playlists, self.list_playlists.GetId(), self.OnItemSelected)
        EVT_BUTTON(self.button_ok, self.button_ok.GetId(), self.OnOK)
        def OnOK(self, event):
        print "OK"
            def OnItemSelected(self, event):
        print "OnItemSelected"

Hi sashan,

then I don't know.
The example I gave works fine for me but
I don't use XRC resources.
So there maybe a difference there.

Regards
   Adi

Quoting sashan <sashang@ihug.co.nz>:

···

sashan wrote:

> Adi Sieker wrote:
>
>> Hi Sashan,
>>
>> sashan wrote:
>>
>> [snip]
>> > EVT_LIST_ITEM_SELECTED(self.list_playlists,
>> > XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
>> > EVT_LIST_ITEM_ACTIVATED(self.list_playlists,
>> > XRCID("LIST_PLAYLISTS"), self.OnItemActivated)
>> >
>> Try this:
>> EVT_LIST_ITEM_SELECTED(self,
>> XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
>>
>> The way I understand the events is that the first parameter is
>> object which contains the can handle the event not the object which
>> emits the event.
>> The second paramter is the wxWindows ID of the objects which emits
>> the event.
>
>
>
> I tried the way you suggested but it didn't make a difference. I had a
> look at how boa-constructor handled list boxes and the 1st parameter
> is the control that generates the event. For example:
> EVT_LIST_ITEM_SELECTED(self.listCtrl1, wxID_WXDIALOG1LISTCTRL1,
> self.OnListctrl1ListItemSelected)
>
> I'm sure the Id / /am passing to/ /the event handler is right because
> self.list_playlist.GetId() == XRCID("LIST_PLAYLISTS")
>
Also I think the way you suggest should also work since the example over here
http://wiki.wxpython.org/index.cgi/UsingXmlResources uses that method for the
event handlers for the button.

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

sashan wrote:

I've attached my source code for the init function of the dialog class I'm trygin to create. I can't see where I'm going wrong. The events don't work (i.e. OnItemSelected and OnItemActivated aren't called.

class DlgPlaylists(wxDialogPtr):
    def __init__(self, parent, db_cursor):
        print "init"
        res = wxXmlResource("./dialog_playlists.xrc") dlg = res.LoadDialog(parent, "DLG_PLAYLISTS") wxDialogPtr.__init__(self, dlg.this)
        self.db_cursor = db_cursor
        self.list_playlists = XRCCTRL(self, "LIST_PLAYLISTS")
        ##Query the database for all the playlists
        self.db_cursor.execute("select * from playlists;")
        playlists = self.db_cursor.fetchall() for playlist, _ in playlists:
            self.list_playlists.Append(playlist)

        EVT_LIST_ITEM_SELECTED(self.list_playlists, XRCID("LIST_PLAYLISTS"), self.OnItemSelected)
        EVT_LIST_ITEM_ACTIVATED(self.list_playlists, XRCID("LIST_PLAYLISTS"), self.OnItemActivated)
           def OnItemSelected(self, event):
        print "OnItemSelected"

    def OnItemActivated(self,event):
        print "OnItemActivated"

I don't see any problems with the above. Please reduce it to a small runnable sample that shows the problem and somebody will take a closer look.

···

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

I don't see any problems with the above. Please reduce it to a small runnable sample that shows the problem and somebody will take a closer look.

Ok...I've attached a resource file and code to load and run the resource file. The button event works as expected but the list selected event doesn't. The file uses tab characters for spacing (sorry if this annoys some people). You can run it by typing

python ./list_ctrl_event.py

at the command prompt

list_ctrl_event.py (944 Bytes)

dlg_list_ctrl.xrc (1.4 KB)

···

--
sashan
http://www.cs.auckland.ac.nz/~sgov008

sashan wrote:

I don't see any problems with the above. Please reduce it to a small runnable sample that shows the problem and somebody will take a closer look.

Ok...I've attached a resource file and code to load and run the resource file. The button event works as expected but the list selected event doesn't.

You are using the wrong event. Your XRC is using a wxListBox, but EVT_LIST_ITEM_SELECTED is for wxListCtrl's. If you change to EVT_LISTBOX then the handler is called.

···

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