Python Forum
If error sleep and then try again from there
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If error sleep and then try again from there
#1
Hey!

So I wrote a long code which is using the pyautogui locatonOnScreen and then center and then click functions. Basically it has a picture of a button which it finds on screen and clicks there. I have around 40 of these in the code to find specific buttons in a program. My problem is that sometimes the script goes too fast and it already tries to search for the image even though technically it has not appeared yet. We are talking about fractions of a second but randomly it give me the "picture not found" error depending on how fast my computer is at the moment and where the code gets to.

So my question is, can I somehow tell the whole code to use "time.sleep(3)" (wait 3 seconds before proceeding) if it encounters and error during the code and continue from the same line without adding a "try and else" after every line?
Reply
#2
Sure.
import time

def do_work():
    # whatever you're doing

while True:
    try:
        do_work()
    except:
        time.sleep(3)
Reply
#3
Wow, i never knew you could do it this way! Thank you for the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use subprocess.Popen and time.sleep chucky831 2 1,890 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Can you end the Time.sleep function boier96 9 9,222 Jan-16-2021, 10:09 PM
Last Post: Serafim
  time.sleep mtnwinds 4 2,796 May-21-2020, 10:12 AM
Last Post: Larz60+
  time.sleep works erratically, a bug in Python? stipcevic 2 3,819 Jan-21-2020, 09:38 PM
Last Post: Marbelous
  Adding Cosinus as utime.sleep variable Kreszy 0 1,694 Sep-19-2019, 04:27 PM
Last Post: Kreszy
  How to achieve close to 1ms time.sleep kwekey 1 14,806 Mar-25-2019, 02:52 AM
Last Post: Larz60+
  time.sleep(...) problem DPaul 3 2,744 Jan-28-2019, 11:40 AM
Last Post: Larz60+
  Alternative for time.sleep()? Atkion 1 7,443 Jan-15-2018, 10:25 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020