You can do this using a while loop like this:
from time import sleep
import pyautogui
pyautogui.PAUSE = 2.5
while True:
sleep(2)
pyautogui.moveTo(1171, 458)
pyautogui.click()
pyautogui.moveTo(1123, 403)
pyautogui.click()
If you don't want the script to sleep for 2 seconds every time, you can move the sleep(2) above the while True: and remove the spaces behind the sleep(2)
Hi guys, i create script on python and i want that it repeat endlessly. I dont know have implemented loop for this because always appear mistake :( I will show code below.
import pyautogui
import time
print(pyautogui.size())
pyautogui.moveTo(1800,1000)
pyautogui.click()
pyautogui.typewrite('F4F')
pyautogui.moveTo(1890,1000)
pyautogui.click()
time.sleep(3)
pyautogui.moveTo(1800,1000)
pyautogui.click()
pyautogui.typewrite('Oddam!')
pyautogui.moveTo(1890,1000)
pyautogui.click()
time.sleep(3)
pyautogui.moveTo(1800,1000)
pyautogui.click()
pyautogui.typewrite('ODDAM OBSERWACJE')
pyautogui.moveTo(1890,1000)
pyautogui.click()
time.sleep(3)
pyautogui.moveTo(1800,1000)Please help me with this homies.
How can I loop this code every 20 secondes?
The time modele offers a method you want:
import time time.sleep(20)
time.sleep(20) a will delay your code by 20 seconds Documentation
And you can put you code into a while-loop:
import time
while True:
# do something
time.sleep(20)
# if you want to exit the loop you can use this keyword:
break
The keyword break stops the while-loop.
Alternatively, you could solve the problem this way:
go_ahead = True
while go_ahead:
# do something
time.sleep(20)
go_ahead = False More on reddit.com python - how to break the while loop in pyautogui by clicking on specific keyboard key - Stack Overflow
python - Pyautogui Script and Looping - Stack Overflow
python - How can I loop PyAutoGUI's locateCenterOnScreen until the image is found? - Stack Overflow
Videos
I have made this code and for it to function It needs to be looped every 20 secondes.
import pyautogui
import cv2
pyautogui.FAILSAFE = True
pyautogui.dragTo(1095, 568, 2)
pyautogui.click(1095, 568, 5) #De 0,01 knop
pyautogui.dragTo(720, 644, 2)
pyautogui.click(720, 644) #De ct knop
if pyautogui.locateOnScreen("ctt.png", confidence=0.4, region=(1220, 487, 32, 30)):
pyautogui.dragTo(1018, 568, 2)
pyautogui.click(1018, 563) #De clear knop
else:
pyautogui.dragTo(1440, 563, 2)
pyautogui.click(1440, 563) #De x2 knopAnd is it possible every every loop so every 20 secondes to look if the image is found? ``pyautogui.locateOnScreen("ctt.png", confidence=0.4, region=(1220, 487, 32, 30))``
I had to do something similar not long ago.
First of all I imported 'Pyautogui' as 'pe' and also I imported time.
So this is what I did.
waitingloop = 1
I defined a waiting loop that is equal to one, and then using that I wrote
while waitingloop == 1:
if pe.locateCenterOnScreen('signinbox.png') == None:
time.sleep(0.5)
else:
time.sleep(0.5)
pe.typewrite(email)
pe.hotkey('enter')
break
So as you can see, with the 'waitingloop' this process will be running all the time, and then everytime IT FAILS to locate the image and returns 'None' it sleeps for 0.5 seconds and tries to do so again until it actually finds the image, then when it finds it as you can see it just does what I wanted it to do (in your case it would be 'click') and once it does that the loop breaks.
I hope this code can help you.
I know this question is about a year old, but the other answer is no longer relevant since locateCenterOnScreen() does not return None anymore, so this answer is for anyone who looked this up.
At the start of the program you must have this function.
pyautogui.useImageNotFoundException()
This code loops infinitely with a try-catch until locateCenterOnScreen no longer raises ImageNotFoundException.
while True:
try:
x, y = pyautogui.locateCenterOnScreen()
break
except pyautogui.ImageNotFoundException:
pass
From the documentation:
Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, a bug in your program could make it go out of control. It’s hard to use the mouse to close a program if the mouse cursor is moving around on its own.
As a safety feature, a fail-safe feature is enabled by default. When a PyAutoGUI function is called, if the mouse is in any of the four corners of the primary monitor, they will raise a pyautogui.FailSafeException. There is a one-tenth second delay after calling every PyAutoGUI functions to give the user time to slam the mouse into a corner to trigger the fail safe.
You can disable this failsafe by setting pyautogui.FAILSAFE = False. I HIGHLY RECOMMEND YOU DO NOT DISABLE THE FAILSAFE.
The tenth-second delay is set by the pyautogui.PAUSE setting, which is 0.1 by default. You can change this value. There is also a pyautogui.DARWIN_CATCH_UP_TIME setting which adds an additional delay on macOS after keyboard and mouse events, since the operating system appears to need a delay after PyAutoGUI issues these events. It is set to 0.01 by default, adding an additional hundredth-second delay.
Therefore, if you want to "speed up" your loop, you can reduce the pyautogui.PAUSE value. However, keep in mind, this will prevent you from having time to activate the failsafe if you need it.
Set pyautogui.PAUSE to a small number. The default is 0.1 secs between actions.
Here is example code:
import pyautogui
pyautogui.PAUSE = 0.01 # can be a float or an integer
This will increase the speed of your spambot/autoclicker/anything.
I'm trying to make custom macro keys with pynput and pyautogui but I can't get pynput to work in a loop without errors or unexpected results. I've tried digging through the github code but I can't figure out how it all works.
My end goal is to be able to hit a key or combination of keys on the keyboard (e.g. Numpad_1 or page_up or ctrl+h) and it run a script with pyautogui like a custom macro. I could do this with a program like AHK but I want to do it with python for more flexibility and familiarity. I don't mind doing a bit of Java if I need to or if there's a better way to do it in Java.
I'm running Python 3.4.3 on Windows 10 from the command prompt but I don't want the command prompt focused so I can run the macro while running another program like Photoshop or something. Here's my code so far that doesn't seem to work:
import time
from pynput import keyboard
def on_press(key):
try:
print('{}'.format(key.char))
except AttributeError:
print('{}'.format(key))
def on_release(key):
if key == keyboard.Key.delete:
# Stop listener
return False
# Collect events until released
'''
with keyboard.Listener(on_press = on_press, on_release = on_release) as listener:
listener.join()
'''
listener = keyboard.Listener(on_press = on_press, on_release = on_release)
listener.start()
i = 0
while True:
listener.join()
time.sleep(1)
print(str(i) + ' times')
i += 1
listener.stop()
This code is mostly a keylogger like the ones found on every tutorial site made for pynput. What I want this to do is run the keylogger in the loop so I can have other code in the loop constantly running (like the counter I have now) but still have keypresses registered. How would I do this?
Also, I'm confused on what listener.join(), with keyboard.Listener(...) as listener:, listener.start() and listener.stop() do. If anyone can help me with explaining anything, I really appreciate it.