To get this to work properly, you will need to catch key events and check to see if the user has pressed the Enter or Tab keys. As @sundar as already mentioned, to get tabbing to work correctly on all platforms, the widgets need to be children of a panel. Here's a fairly simple example:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.text = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
self.text.Bind(wx.EVT_KEY_DOWN, self.onEnter)
btn = wx.Button(self, label="Do something")
self.text.SetFocus()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def onEnter(self, event):
""""""
keycode = event.GetKeyCode()
if keycode == wx.WXK_RETURN or keycode == wx.WXK_NUMPAD_ENTER or keycode == wx.WXK_TAB:
self.process_text(event=None)
event.EventObject.Navigate()
event.Skip()
#----------------------------------------------------------------------
def process_text(self, event):
"""
Do something with the text
"""
text = self.text.GetValue()
print text.upper()
for word in text.split():
print word
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="TextCtrl Demo")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Here we bind to wx.EVT_KEY_DOWN
and have it extract the keycode that was pressed. Then it checks to see if the keycode is the Enter or Tab key. If it is, it calls a function to process the text and then it calls event.EventObject.Navigate()
, which will cause wxPython to move the focus to the next widget in the tab order.
You might want to read the following about that subject:
For more information on wxPython's key and char events, see the following: