Problems with setting Background Colour of TextCtrl widgets

I’m currently writing a front end for a science app which needs the user to key in numbers for various parameters. I’m using a validator to catch the input from a TextCtrl, and shade it pink for invalid or default for valid, using SetBackgroundColour on the TextCtrl. However, the GUI’s not updating correctly. Please see the attached image file for an example.

State 1 is what happens when we start with all valid input.

State 2 is what happens when invalid data is entered in a widget. The background colour has changed, but the border is still white.

State 3 is what happens if you then mouse over the widget. The border is redrawn to match the background colour.

State 4 is what happens if you correct the value. The background colour is back to white, but the border is still pink.

If you mouse-over the widget in State 4, you’ll be back at State 1.

I’ve tried calling both Refresh and ClearBackground on the TextCtrl, but neither of them seems to work.

Any suggestions? I saw a similar thread from 2005 that came up with no solution, but said a patch would be going in at some point.

Windows XP 32-bit, Python 2.6.6, wxPython 2.8 Unicode.

Cheers,

Alex

borders.png

Actually, I have found a hacky solution:

ctrl.SetBackgroundColour(‘Pink’)
ctrl.Hide()
ctrl.Show()
ctrl.SetFocus()

This works, and doesn’t seem to cause flicker. Undeniably bodgy, though.

Any alternative suggestions would be gratefully received.

Cheers,

Alex

Did you try ctrl.Refresh() or maybe Refresh the ctrl’s parent?

  • Mike

My guess is that Windows is only applying the Refresh() to the client area of the widget, and that the border area is outside of the client area. What you've done above is causing the border area to be invalidated so it is refreshed as well.

As Mike suggested calling the parent's Refresh may help, but that may give some flicker as well. Wrapping it in Freeze()...Thaw() may work, as I think it does a more aggressive refresh when thawing. Otherwise, if the above works then it probably doesn't hurt anything to keep it.

···

On 6/1/11 8:54 PM, Alex Holland wrote:

Actually, I have found a hacky solution:

ctrl.SetBackgroundColour('Pink')
ctrl.Hide()
ctrl.Show()
ctrl.SetFocus()

This works, and doesn't seem to cause flicker. Undeniably bodgy, though.

--
Robin Dunn
Software Craftsman