Search code examples
eventslistboxwxpython

wxPython ListBox event not firing


I have a wx.Dialog that contains a button and 2 ListBoxes, the button findAppBtn searches through a list of directories and then displays the result in actListBox. Selecting the directory of your choice from actListBox should then fire the event EVT_LISTBOX which calls actListBoxList. This function does an ls on the directory and should list the files it finds in the lower list box binListBox using Append. Upon selecting an item from the lower ListBox, the window closes.

The problem is that the self.Bind(EVT_LISTBOX, self.actListBoxList) does not seem to be firing when an item is selected.

(also please excuse the bad coding, I am trying to get it working before minifying)

    self.findAppBtn = wx.Button(panel, -1, "Find app")
    self.findAppBtn.SetDefault()
    self.Bind(wx.EVT_BUTTON, self.startConf, self.findAppBtn)
    hBox2.Add(self.findAppBtn, 0, flag=wx.LEFT, border=5)
    vBox.Add(hBox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=3)

    self.actListBox = wx.ListBox(panel, choices=[])
    self.Bind(wx.EVT_LISTBOX, self.actListBoxList)
    vBox.Add(self.actListBox, 2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=3)

    self.binListBox = wx.ListBox(panel, choices=[])
    self.Bind(wx.EVT_LISTBOX, self.binListBoxList)
    vBox.Add(self.binListBox, 2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP|wx.BOTTOM, border=3)

    self.closeBtn = wx.Button(panel, wx.ID_OK)
    hBox4.Add(self.closeBtn, 0, flag=wx.LEFT, border=5)

    vBox.Add(hBox4, 0, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=5)

    panel.SetSizer(vBox)


def startConf(self,e):
    val = self.cmdTxt.GetValue().replace(" ","\ ")
    path = "/private/"
    aCmd = "find " + path + " -iname '*"+val+"*.app'"
    try:
        s = pxssh.pxssh()
        s.login(sshIP, "root", sshPort, sshPass)
        s.sendline(aCmd)
        s.prompt()
        AP = s.before
        for m in AP.split('\n'):
            if path in m:
                self.actListBox.Append(m.replace(path,"").strip())
        s.logout()
        return path
    except pxssh.ExceptionPxssh as e:
        self.parent.progressBox.AppendText(str(e))


def actListBoxList(self,e):
    #get string from top box selection e.g xxxx-xxxx-xxxx-/myapp.app
    selName = self.actListBox.GetStringSelection() 
    path = "/private/"
    #list all the files in the dir from top box selection
    aCmd = "ls " + path + selName
    try:
        s = pxssh.pxssh()
        s.login(sshIP, "root", sshPort, sshPass)
        s.sendline(aCmd)
        s.prompt()
        ls = s.before
        for file in ls.split('\n'):
            if not file.endswith("/"):
                reg = r"\..*"
                matchObj = re.search(reg, file)
                if not matchObj:
                    self.binListBox.Append(file)
        s.logout()
    except pxssh.ExceptionPxssh as e:
        self.parent.progressBox.AppendText(str(e))

def binListBoxList(self,e):
    binaryName = self.binListBox.GetStringSelection()
    self.Close()

EDIT: self.actListBox.Bind(wx.EVT_LISTBOX, self.actListBoxList) fixed the issue.


Solution

  • calling self.Bind(... binds the event to the parent window which is why you're not seeing the event being called. Bind to the listbox instead:

    self.actListBox.Bind(wx.EVT_LISTBOX, self.actListBoxList)