Flash the background

I would like to flash the background of a Text Field when that enter more characters then the max allowed. This is what I have right now:

import time
from java.awt import Color
TextField = event.source.parent.getComponent('Text Field')
startingColor = TextField.background
errorColor = Color(255,128,128)

if event.propertyName == 'text':
	print len(event.newValue)
	if len(event.newValue)>event.source.parent.MaxCharacters:
		event.source.text = ''
		TextField.background = errorColor
		sleep(5)
		TextField.background = startingColor

I found that this wont work becuase of the way the time function fires but can not think of another way to do it, any suggestions?

Definitely don’t call sleep from a GUI thread.

I would use an expression binding on the Text Field background instead. You could look at the current time in seconds and use a modulo operator to accomplish what you want.

Edit: Sounded like fun so I just went ahead and did it myself.

if((len({Root Container.Text Field.text}) > {Root Container.Text Field.MaxCharacters}) &&
(getSecond(now(1000)) % 2) ,
Color(255,128,128) , "White")

As a different approach, I changed the custom property to hold the length of the text. The style customizer than then be used to adjust setpoints and animations.

Thank you both for you replies. going to try this now.

To expand on the sleep() thing, because I didn't know this and almost did a bad with the project I'm working on:

That thread gives the reasons why you shouldn't use sleep() and a few workarounds