Suppressing an event

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

Miki Tebeka wrote:

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?

Generally you would set a flag before the function that causes the event and then check that flag in the event handler, but in this case you can fix it simply by catching EVT_KEY_DOWN instead of _UP. The key events go to whatever window has the focus and since you set the focus to the listbox in the EVT_TEXT_ENTER (which is generated from the default EVT_KEY_DOWN) by the time the _UP happens the focus is on the other control so that is where the event goes to.

···

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