简体   繁体   中英

sikuli (python) script starting at xx hour of the day, wait(“image.png”,highnumber) not working?

first i'd like to thanks everyone who can help me

i've been using a program that automated some boring features in a game i like, but it's not working anymore.

the thing i want it to do is really stupid, but am not getting good results.. so

1) since i need to run it at a precise time i've found this in the forums and tweaked a very little bit to set it to start around 11 am:

from datetime import datetime  
from threading import Timer

x=datetime.today()  
y=x.replace(day=x.day, hour=11, minute=0, second=20, microsecond=0) 
delta_t=y-x

secs=delta_t.seconds+1

def something():  
    ***Script below***

t = Timer(secs, something)
t.start()

so, is this first part correct ?

2) second part is the actual script, that something is actually something like this:

for x in range (0, 50):
    wait("im0.png"),25)  
    click("im0.png")
    wait("im1.png",15)
    if exists("im2.png"):
        click("im2.png")
    if exists("im3.png",50)
        type("something")
    x +=1

as you can see i set it to wait (or exists) an image for 25 (15 and 40) seconds, but i have the feeling it's not waiting for that time i set it to, i can't try the script often, but i think it tangles around the wait for image commands, saying something like "FindFailed xxx.png" and so on

i'd like it to check every second if the image is present then do the action below (cause it will appear, and i don't want it to skip anything), should i change it in something like loops of "if not exists" then it checks itself again? (input of this kind would be appreciated, am not so good at scripting in python)

could anyone help me write it in a better/right way ?

There are more ways to Rome, so there is not 1 good answer but there are more.
If you would like to make a simple loop that does something at a given time you could simply use:

import time

image1 = ("image1.png")
alarm = '17:37:00'

while (True):
    # Look what time it is right now [24 hour format]. 
    currentTime = time.strftime("%H:%M:%S")
    if (currentTime == alarm):
        print('It is time!')
        # Exit the (while) loop.
        break;

For the second part you could also make us of while not exists() To make it more of a complete script you could use:

import time

image1 = ("image1.png")
image2 = ("image2.png")
image3 = ("image3.png")
alarm = '17:37:00'

class Robot():
    # Automaticly executes when class is called. 
    def __init__(self):
        # Wait until it is time for the alarm to go off. 
        while (True):
            # Look what time it is right now [24 hour format]. 
            currentTime = time.strftime("%H:%M:%S")
            if (currentTime == alarm):
                print('It is time!')
                # Call another definition. 
                self.startWork()
                # Exit the (while) loop to finsih the script.
                break;

    # Find and click the image. 
    def startWork(self):
        while not exists(image1):
            # We didn't see the image, sleep for 1 second. 
            wait(1)
        # When the code gets here, image1 has appeared. 
        imageLoc1 = find(image1)
        imageLoc1.click()
        # Now we are going to look if we see eather image2 or image3
        while not exists(image2) or not exists(image3):
            wait(1)
        # Image2 or image3 is seen. 
        if exists(image2):
            imageLoc2 = find(image2)
            imageLoc2.click()
        elif exists(image3):
            imageLoc3 = find(image3)
            imageLoc3.click()

# Run class 
Robot()

To make the loop while not exists() to also end after a certain time when the image does not appear, you could use:

i = 0
while not exists(image1):
    i = i + 1
    wait(1)
    # After 60 seconds break the loop anyway. 
    if (i == 60):
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM