rje
rje

Reputation: 6418

Enter key in wx.TextCtrl on Mac has no effect

When I run the program below on Mac OS X Yosemite, pressing the enter key inside the TextCtrl doesn't have any effect on the contents of the TextCtrl at all (I need it to enter a newline in the text).

Adding or removing the TE_PROCESS_ENTER style doesn't have any effect at all; the EVT_TEXT_ENTER event doesn't get fired. Pressing enter does trigger an EVT_KEY_UP event with keycode 13, by the way.

Strangely enough, pressing Ctrl+Enter does cause a newline to be entered inside the TextCtrl, but it also doesn't fire an EVT_TEXT_ENTER event.

What is happening here? Of course I could work around this and detect keycode 13, but of course that doesn't really solve the problem.

#!/usr/bin/env pythonw

import wx

class MainWindow(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(500,500))
        self.tc = wx.TextCtrl(self, wx.TE_MULTILINE|wx.TE_PROCESS_ENTER, size=(200,100))
        self.tc.Bind(wx.EVT_KEY_UP, self.OnKeyUp, self.tc)
        self.tc.Bind(wx.EVT_TEXT_ENTER, self.OnEnter, self.tc)
        self.Show(True)

    def OnKeyUp(self, event):
        print event.GetKeyCode()

    def OnEnter(self, event):
        # Never gets called
        print "enter!"

if __name__ == '__main__':
    app = wx.App()
    frame = MainWindow('Test')
    app.MainLoop()

I'm on Mac OS X (Yosemite), using python 2.7.9 (through homebrew) and wxPython 3.0.2.0.

Upvotes: 2

Views: 1698

Answers (1)

RobertoP
RobertoP

Reputation: 637

the problem is in the call to the wxTextCtrl constructor; the flags are not being passed in correctly.

self.tc = wx.TextCtrl(self, wx.TE_MULTILINE|wx.TE_PROCESS_ENTER, size=(200,100))

should really be

self.tc = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER, size=(200,100))

The text control was not being initialized to be a multi-line control.

Upvotes: 3

Related Questions