Coming from # Focus on Text Box Loses Cursor - is there any better solution then specifying Sleep function ? Its the worst way to solve problem like this. Or can anyone explain what the flow of execution of scripts in embedded view so the sleep can actually be considered as good practice here ?
To explain the problem really short: We have a Page with a button which triggers a View in this view there is an embedded component in which is text input. This text input should be focused when the View loads. But except adding sleep to the embedded component to focus the cursor, we could not get it work.
Thanks.
I think ones I read that the instant script flow control is asynchronous, and you can not really control the order of execution.
Have you tried to set focus in onStartup event? I would try to put the script in the view or in the component itself.
Regarding the sleep action, I would use instead:
from threading import Timer
def focus():
self.focus()
Timer(0.5, focus).start()
Thanks but I am looking for some other way, e.g. checking if Component exists, or something similar to javascript like onDocumentReady. Stopping time or waiting certain amount of time would resolve to unexpected behavior I guess.
As I told you have you tried to place the script on onStartup event?. Do it on the component itself.
self.focus()
or do it in a view script
try:
self...focus()
except:
pass
for me it works great, without any timer
![image](https://global.discourse-cdn.com/inductiveautomation/original/3X/6/2/621d0b75a74824f9ed5090c688f3a360c28bfde8.png)
We tried that but did not work, remember its an embedded view
How if you give focus to the embedded view first and then give focus to component inside
Nope, does not work, I tried this
This is embedded view - onStartup script
![image](https://global.discourse-cdn.com/inductiveautomation/original/3X/a/a/aa2b47cbb2f1e04d31bf6b397f1baf2b4694ca5c.png)
and this is component inside embedded view I want to have focused