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
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,980 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  Use subprocess.Popen and time.sleep chucky831 2 3,724 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Can you end the Time.sleep function boier96 9 21,422 Jan-16-2021, 10:09 PM
Last Post: Serafim
  time.sleep mtnwinds 4 5,319 May-21-2020, 10:12 AM
Last Post: Larz60+
  time.sleep works erratically, a bug in Python? stipcevic 2 7,128 Jan-21-2020, 09:38 PM
Last Post: Marbelous
  Adding Cosinus as utime.sleep variable Kreszy 0 2,544 Sep-19-2019, 04:27 PM
Last Post: Kreszy
  How to achieve close to 1ms time.sleep kwekey 1 23,549 Mar-25-2019, 02:52 AM
Last Post: Larz60+
  time.sleep(...) problem DPaul 3 4,399 Jan-28-2019, 11:40 AM
Last Post: Larz60+
  Alternative for time.sleep()? Atkion 1 9,640 Jan-15-2018, 10:25 PM
Last Post: metulburr

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.