Hi guys
I am making a program that uses a text control to move messages from one place to another. My program needs to be able to, once the message has been moved, delete what is in the textCtrl. I can go:

self.textCtrl.SetValue('')

but that leaves me with a problem as the next time i put my cursor in the text control it is one line down. It added a whole line! So i was wondering how i would combat this so that the textctrl would be reset to have nothing in it?

Dani AI

Generated

Short summary and root cause: the behaviour you saw (the control ending up with an extra blank line or the caret not where you expect) is usually caused by programmatic text changes triggering the control's text-change handling so other code runs and alters the control state. That includes handlers bound to text events and any UI code that runs in response to those events. 's insertion-point idea can be useful for scrolling, and 's SendKeys workaround will work, but there are cleaner, more reliable APIs for both clearing and appending text. (docs.wxpython.org)

Cleaner ways to clear text (and avoid event handlers firing): use the API that changes the value without generating the text event. This prevents any EVT_TEXT callbacks from reacting to the programmatic change. Example (concept only):

self.textCtrl.ChangeValue('')

If you do want the event to fire (so other logic runs), use the regular clear method instead — but be aware that will invoke handlers. (docs.wxpython.org)

Appending and keeping the view scrolled to the end: use the control's append/write methods and the dedicated "set insertion point to end" helper so the caret is placed at the bottom and the control scrolls for you. Example:

self.textCtrl.AppendText(new_text)
self.textCtrl.SetInsertionPointEnd()

That is more robust than synthesising keystrokes to force scrolling. (docs.wxpython.org)

Other practical tips: avoid SendKeys/global keystroke hacks — they are platform dependent and fragile. If your code still misbehaves, add a simple guard when you change text programmatically (set a boolean flag before/after the change and have your EVT_TEXT handler ignore changes when the flag is set). For bulk updates or many small edits, freeze the window before making changes and thaw afterward to avoid flicker and unexpected redraws. (stackoverflow.com)

These approaches keep updates deterministic, avoid race conditions caused by event handlers, and remove the need for brittle workarounds.

Recommended Answers

All 7 Replies

Could you use EmulateKeyPress() after that to send a backspace to it?

Chris

No i dont think so.

Emulate key press required an event. I think that means somethign like wx.EVT_TEXT or something. Am i wrong? Can you use the backspace character or some kind of ascii key code?

Hmm i see your point, i cannot find a method of creating a wxKeyEvent event which is what is required for EmulateKeyPress().

Another possible solution would be to use

self.textCrtl.SetInsertionPoint(0)

Or you could use this method to clear the textcontrol which should work perfectly

self.textCtrl.Remove(0, self.textCtrl.GetLastPosition()+1)

Hope that helps

Chris

Okay both of those did not actually do anything different from my first idea. So i went with the SendKeys module and sent the backpace key once so that the cursor would be taken back to the start again.

I do have one last issue though, that is, this text that gets entered in this textCtrl gets pasted up into another, this slowly accumulates. Soon though the text starts to need a scroll bar. My problem is that the text dissapears at the end of the TextCtrl. I want to be able to auto-scroll or something like that but i cant find out how to make it automatically always scroll down to the end.

Hmm thats odd. I did think about the SendKeys module but i wasn't sure if you wanted to use it or not.

Addressing your other issue, did you try using self.textCtrl2.SetInsertionPoint(self.textCtrl2.GetLastPosition()+1) , i cannot say as i've tried it but that should theoretically force it to scroll to the bottom.

Chris

commented: Thanks for that +1

Yep, that works a charm. I tried something simillar before but it didnt work. Oh well, that all works now, thanks Chris!

Yep, that works a charm. I tried something simillar before but it didnt work. Oh well, that all works now, thanks Chris!

Hehe glad you got it working.

To think i've only ever downloaded wxPython & created a basic window with a listbox on it. I've never even added data to that list box hehe. Ah well it's all good fun.

P.S Mark as solved xD

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.