Hello. I haven't figured out how to trap the enter key in a listbox. I
have a dialog with a listbox and the standard OK/Cancel buttons. I can
detect a double-click in the listbox, and I can detect the enter key
when the user hits the OK button, but I'd really like to detect the
enter key when the user is still in the listbox. I am running Linux.
Here is the relevant code:
from wxPython.wx import *
···
#---------------------------------------------------------------------------
class RecentChanges:
def layoutDialog(self, lb, ok, cancel):
box1 = wxBoxSizer(wxHORIZONTAL)
box1.Add(lb, wxEXPAND, wxEXPAND)
box2 = wxBoxSizer(wxVERTICAL)
box2.Add(ok, 0, wxEXPAND | wxALL, 5)
box2.Add(cancel, 0, wxEXPAND | wxALL, 5)
box = wxBoxSizer(wxHORIZONTAL)
box.Add(box1, wxEXPAND, wxEXPAND)
box.Add(box2, 0, wxEXPAND)
self.win.SetSizer(box)
self.win.SetAutoLayout(true)
def listBox(self, win, items):
id = wxNewId()
lb = wxListBox(win, id, wxPoint(80, 50), wxDefaultSize,
items, wxLB_SINGLE)
EVT_LISTBOX_DCLICK(win, id, self.EvtListBoxDclick)
return lb
def chooseFile(self):
item = self.lb.GetSelection()
self.fn = self.recentChanges[item][0]
self.win.EndModal(wxID_OK)
def EvtListBoxDclick(self, event):
self.chooseFile()
def OnOkButton(self, event):
self.chooseFile()
def run(self, frame, recentChanges):
self.win = win = wxDialog(frame, -1, "This is a wxDialog",
wxDefaultPosition, wxSize(450, 200))
self.recentChanges = recentChanges
items = [x[0] for x in recentChanges]
self.lb = self.listBox(win, items)
ok = wxButton(win, wxID_OK, " OK ")
cancel = wxButton(win, wxID_CANCEL, " Cancel ")
ok.SetDefault()
self.layoutDialog(self.lb, ok, cancel)
EVT_BUTTON(win, wxID_OK, self.OnOkButton)
self.fn = None
val = win.ShowModal()
if val == wxID_OK:
return self.fn
else:
return None