Take the 2-minute tour ×

I have a shell-like console for my app, which prompts with ">>>" after every command. The problem is every time I have my shell WriteText(">>> "), it also appends a new line. The user can backspace up to the correct line, but this just looks terrible. Any way to fix it?

share|improve this question
Forgot to mention, this is a wxpython TextCtrl.WriteText() operation –  Shaunak Mar 5 '11 at 1:15
(Something somewhere is adding a newline... find out where it is :p) –  user166390 Mar 5 '11 at 3:53
Tried it. Yes really awful. Got no solution for it –  joaquin Mar 5 '11 at 10:23
add some minimal code demonstrating the problem –  Anurag Uniyal Mar 6 '11 at 4:10
add comment

1 Answer

up vote 4 down vote accepted

I suspect you are declaring your TextCtrl to be of style wx.TE_PROCESS_ENTER and then binding the EVT_TEXT_ENTER event - only because I ran into the same problem when I tried that.

My first instinct was to write a method to process wx.EVT_TEXT_ENTER that would then use the TextCtrl:Remove() method. This method only seems to remove visible characters, unfortunately. My next thought was to use the EmulateKeyPress() method along with backspace (WKX_BACK) to erase the newline character. This might be doable though I couldn't come up with a good way to spoof a wx.KeyEvent (can't just use event.m_keyCode since EVT_TEXT_ENTER sends a CommandEvent, and not a KeyEvent) so I gave up on that approach... err, I mean this solution is left as an exercise to the reader.

wx.EVT_TEXT_ENTER being a CommandEvent finally led to a third angle which did work. Instead of binding wx.EVT_TEXT_ENTER, I bound wx.EVT_CHAR and put in logic with special processing for the Return key (ASCII key code 13). I then planned to implement the EmulateKeyPress() bit I talked about earlier but when I removed wx.TE_PROCESS_ENTER from the TextCtrl style, I discovered that the \n was no longer being surreptitiously added. Code follows:

import wx

class TestRun(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self, parent, title="StackO Test", size=(400,400))
        self.control = wx.TextCtrl(self, id=wx.ID_ANY, style=wx.TE_MULTILINE)
        self.control.Bind(wx.EVT_CHAR, self.OnPress)


    def OnPress(self, event):
        if event.GetKeyCode() == 13:

if __name__ == '__main__':
    app = wx.App(False)

The event.Skip() line is crucial; during my research into this, I learned that a KeyEvent is usually followed by a CharEvent. The CharEvent is the part where the character is written to the TextCtrl. When you intercept the KeyEvent, the CharEvent is only called if you do so explicitly - event.skip() is therefore vital if you want your TextCtrl to otherwise behave as normal. Without it, any keyboard input not equal to ASCII keycode 13 would do nothing.

From my tests, it appears that there is something about declaring the TextCtrl to have style wx.TE_PROCESS_ENTER that makes it print a newline after each call to WriteText(). My way gets around this though you will have more work to do with regard to making sure the insertion point is always in the right place, etc.

Best of luck!

share|improve this answer
Yea figured this out after a little tinkering, but great answer. –  Shaunak Mar 25 '11 at 18:17
add comment

Your Answer


