Hello All,
I've written a simple dialog where the user enters a query and get some
result back. When double clicking/hitting ENTER on the result some action
will happen.
The problem was that when trying to be nice and move the focus to the
result ListBox the ENTER event from the query TextCtrl is caught by the
ListBox. Any way to suppress this event?
wxPython 2.5.2.8 and Python 2.3.4 on winXP.
Here's a short example, after writing something in the query TextCtrl and
hitting ENTER the error dialog will pop up.
--- query.py ---
import wx
class QueryDlg(wx.Dialog):
'''Main dialog'''
def __init__(self):
wx.Dialog.__init__(self, None, -1, "")
sizer = wx.BoxSizer(wx.VERTICAL)
# Query panel
self._query = wx.TextCtrl(self, -1, size=(200, -1),
style=wx.TE_PROCESS_ENTER)
self._query.Bind(wx.EVT_TEXT_ENTER, self.on_go)
sizer.Add(self._query, 0, wx.EXPAND)
# Results panel
self._results = wx.ListBox(self, -1, size=(200, 200))
self._results.Bind(wx.EVT_KEY_UP, self.on_key_up)
sizer.Add(self._results, 1, wx.EXPAND)
# Make ESC close us
btn = wx.Button(self, wx.ID_CANCEL, "")
btn.Bind(wx.EVT_BUTTON, lambda e: self.EndModal(wx.ID_CANCEL))
btn.Hide()
# Resize
self.SetSizer(sizer)
self.SetAutoLayout(1)
sizer.Fit(self)
self.CenterOnScreen()
def action(self, str):
'''Do an action'''
wx.LogError(str)
def on_go(self, evt):
'''Handle query'''
# Simulatre query
for i in range(10):
self._results.Append("item %d" % i)
# Set focus on result window
# PROBLEM HERE!!!
# Sadly this will fire an "Enter" event on the selected item
self._results.SetSelection(0)
self._results.SetFocus()
def on_key_up(self, evt):
'''Handle key up for Enter'''
key = evt.GetKeyCode()
curr = self._results.GetSelection()
if curr == wx.NOT_FOUND:
curr = 0
if key == wx.WXK_RETURN: # Enter, view file
self.action(self._results.GetString(curr))
else:
evt.Skip()
# Run the application
app = wx.PySimpleApp()
dlg = QueryDlg()
dlg.ShowModal()
dlg.Destroy()
--- query.py ---
Thanks.
···
--
------------------------------------------------------------------------
Miki Tebeka <miki.tebeka@zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys