Looping infinite with observer

Asked by MP on 2019-04-14

Breaking my head again :)
I didnt want to post and find the answer myself, but reading the docs endlessly and not getting it, here I am again.

code:
=====================

r = Region(1059,318,18,17)

tr = TextOCR.start()
tr.setPSM(6)

def eventstop(event):
    event.stopObserver()

r.onChange(eventstop)
r.observeInBackground(200)
wait(4)

r.highlight(2)
print r.text() #just to check and compare the number with my own eyes
if int(r.text()) < 18:
    print r.text()
    print (" is > 18")

else:
    print r.text()
    print (" is < 18")
==================

I want to infinite loop my code. so that he checks again after he read the previous number.

1) GOTO didnt work :( (I noticed afterwords it doesnt excist in phyton *sad face*
2) wait(IMAGE, FOREVER)
didnt work, I wanted to do infinite looping while a certain images excised on the screen. (I got this from your Desktop Surveillance exercise (I would love to see more of those practical examples)
3) I tryed (FOREVER) but still closed
4) I tryed after the IF and Else statement "r.observe()"
thinking that , I can just start the observer again and he starts again from the top (which didnt work).
5) I noticed with the following code that aslong he finds it , he is infinite. But when he doesnt find it , it closes the script. (which I dont understand why) So I assume I have to do something special with the IF, else statement to get the infinite loop?

while ("1555266479704.png",FOREVER):

    if find("1554910622874.png"):
        popup ("YES")

    else:
        popup ("NO")

I cant sleep, I cant eat, getting depression and nightmares of code. So hopefully you can help me out :) Thank you!

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2019-04-16
Last query:
2019-04-16
Last reply:
2019-04-15
Best RaiMan (raimund-hocke) said : #1

What you are missing is some basic understanding of scripting and here especially with Python.
Every scripting language has some regular constructs, that follow systematic rules, which is usually called its syntax.
Syntax is nothing that can be handled like art work or with creativity, it has simply to be learned.

There are good tutorials in the net, where you can learn the basic syntax elements.

Sikulix is only a feature set, that can be used in various (currently mostly Java aware) scripting and/or programming environments.
Hence the SikuliX docs only try to explain the SikuliX features. Due to historical reasons the examples are mostly in Python, which is the most used variant. But the SikuliX docs cannot help you to learn scripting or even Python.

I now give you some comments to your example:

--- while ("1555266479704.png",FOREVER):
while expects a condition-expressions which either evaluates to False or True.
If True, the loop will start again, otherwise it is stopped (code continues after the while block).

("1555266479704.png",FOREVER) evaluates to a tuple (non-modifyable list), which always evaluates to True.

So the easiest way, to have an endless loop:

while True:

endless loops should always have some code inside, that allow, to end the loop in special situations.

usually:
if someCondition:
    break

Otherwise you have to kill the scriptrun from outside, which should not be a general measure.

--- But when he doesnt find it , it closes the script.
find("1554910622874.png")
throws a FindFailed, if not handled (being the case here), which stops the script with an appropriate error message.
About FindFailed and how to handle it, there is a chapter in the docs.

MP (jozzzzzz) said : #2

Thank you, I gonna delve more into the syntax of python. It is "easier" and more "logical" for me then other programming languages. Every day I start to eat better, sleep better, more happy and less nightmares! ;)

Keep ut the great work you are doing and the amazing support!

RaiMan (raimund-hocke) said : #3

Python surely is the best choice for you, since it only has a few basic principles and huge support in the net, since it is the most used language in the world.

All the best and good to hear, that it is getting better.

MP (jozzzzzz) said : #4

Thanks RaiMan, that solved my question.