Alternative to sleep

Hello everyone, I think this topic is already discussed before, but I can't find the right answer, I hope someone can help me.

I have a script that I turn on to be visible in the label component for 5 sec to let the user know the error.
I'm using something like this below:

def ErrorMsg(vtext):
from time import sleep

self.getChild("Label").meta.visible = True
self.getChild("Label").props.text = "Error during scanning the barcode"
sleep(5)
self.getChild("Label").meta.visible = False
self.getChild("Label").props.text = ""

I would look into using system.util.invokeAsynchronous.

Alternatively, why remove the warning before a successful attempt. I'm aware there may be requirements, but as a user of software I dislike when error messages are transient because I often trigger some action and look away. If the error/warning disappears after some time, I would then look back to my focus area and it would appear no action was taken at all.

Also, why are you suing a Text Field (Input) as a display? Why not use a Label?

Also also, if you're toggling visibility, why even bother clearing the text of the Text Field?

2 Likes

Sorry, changed the post.
It is a Label component.

I will think about your suggestion to keep the error msg thanks.
I'm clearing it because I'm using the same label to display other messages. I could keep it and just change the text to the new one.

In case I would like to have this 5s delay, I was wondering if you could show me how to implement that in this sample code.

Another question is about .focus() I'm using sleep too to make work.

I would make a custom prop somewhere to hold the timestamp of the error event.
You can then use an expression binding like:

secondsBetween({path.to.the.timestamp}, now()) < 5

on the visibility prop.

8 Likes

I like the solution provided by @JordanCClark better than invokeAsynchronous, but here you go:

def runAction(self, event):
	def reset():
		from time import sleep
		sleep(5)
		self.props.text = "Reset"
	self.props.text = "Clicked"
	system.util.invokeAsynchronous(reset)
1 Like