I have a simple one-column grid and it has choice (dropdown) widgets in
each cell. I want the user to be able to make a selection in a cell and
hit Return, thus going down to the next row. Instead, the grid loses
focus and the dialog receives it. Unfortunately, this means the default
button is clicked and the dialog is closed. Not what I was wanting.
NB in Ubuntu I can intercept the wx.EVT_KEY_DOWN event but not in Windows.
Any thoughts?
Here is example code:
import wx
import wx.grid
class dlg(wx.Dialog):
def __init__(self):
wx.Dialog.__init__(self, None)
panel = wx.Panel(self)
szr = wx.BoxSizer(wx.VERTICAL)
self.grid = wx.grid.Grid(panel)
self.grid.CreateGrid(5,1)
for i in range(5):
self.grid.SetCellEditor(i, 0,
wx.grid.GridCellChoiceEditor(["opt 1", "opt 2"]))
btnOK = wx.Button(panel, wx.ID_OK)
btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
szr.Add(self.grid)
szr.Add(btnOK)
panel.SetSizer(szr)
szr.SetSizeHints(self)
self.Layout()
self.grid.SetFocus()
def OnOK(self, event):
wx.MessageBox("OK was clicked")
self.Destroy()
app = wx.PySimpleApp()
frame = dlg()
frame.Show()
app.MainLoop()
All the best, Grant
This is not a complete answer, but may provide a clue.
I had a similar problem when I set up a customised cell text editor. I found
that pressing Enter to leave the editor fired the default button on the
panel, and pressing Tab caused focus to leave the grid and pass to the next
widget on the panel.
The solution was as follows. The following lines are taken from the demo
GridCustEditor.py -
class MyCellEditor(gridlib.PyGridCellEditor):
[...]
def Create(self, parent, id, evtHandler):
self._tc = wx.TextCtrl(parent, id, "")
On Robin's advice, I modified the last line to look like this -
self._tc = wx.TextCtrl(parent, id, "",
style=wx.TE_PROCESS_TAB|wx.TE_PROCESS_ENTER)
In your case you are using a Choice editor. I believe that there is a way of
getting to the TextControl component of the Choice control, but I am not
sure how. If you can get to that, and apply the above style, it may solve
your problem.
HTH
Frank Millman
That gets you to the underlying ComboBox control, which in turn is made up
of a text control and a listbox. I think (not sure) that you need to get a
reference to the text control, so that you can apply the style
wx.TE_PROCESS_TAB|wx.TE_PROCESS_ENTER to it, but I don't know how to do
that.
Frank
>
> On Robin's advice, I modified the last line to look like this -
>
> self._tc = wx.TextCtrl(parent, id, "",
> style=wx.TE_PROCESS_TAB|wx.TE_PROCESS_ENTER)
>
> In your case you are using a Choice editor. I believe that there is a way of
> getting to the TextControl component of the Choice control, but I am not
> sure how.
Not for the stock wx.Choice, it doesn't have a separate wx control for
the text portion :-(
>> NB in Ubuntu I can intercept the wx.EVT_KEY_DOWN event but not in Windows.
>>
>> Any thoughts?
>>
On Windows the keys used for navigation are taken from the stream before
the key-down events, so I expect that the only thing you can do is turn
off the navigation features. I don't know if this will work in a dialog
since it also supports navigation, but try putting the grid and other
widgets in a wx.Panel with style=0, and put that panel in the dialog.
--
Robin Dunn
Software Craftsman
http://wxPython.org
>
> Back to the first approach I mentioned. There has to be a way of
> getting a custom control to do what is required, even in Windows. The
> standard text control allows interception of Returns and Tabs when
> TE_PROCESS_ENTER and TE_PROCESS_TAB are used. Is there some way of
> getting the same sort of thing to happen with other controls? A way
> of preventing the navigation keys from leaving the stream without
> having to disable navigation altogether?
Have you tried using the wx.WANTS_CHARS style?