wxPython ListBox 事件未触发

wxPython ListBox event not firing

我有一个 wx.Dialog,其中包含一个按钮和 2 个列表框,按钮 findAppBtn 搜索目录列表,然后在 actListBox 中显示结果。从 actListBox 中选择您选择的目录应该会触发调用 actListBoxList 的事件 EVT_LISTBOX。此函数在目录上执行 ls,并应使用 Append 在下部列表框中列出它找到的文件 binListBox。从下方的列表框中选择一个项目后,window 关闭。

问题是 self.Bind(EVT_LISTBOX, self.actListBoxList) 在选择项目时似乎没有触发。

(也请原谅糟糕的编码,我试图在缩小之前让它工作)

    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()

编辑:self.actListBox.Bind(wx.EVT_LISTBOX, self.actListBoxList) 修复了问题。

调用 self.Bind(... 将事件绑定到父级 window,这就是为什么您没有看到正在调用的事件。改为绑定到列表框:

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