0
from time import sleep
import pyautogui
from pynput.keyboard import Key, Controller as KeyboardController

keyboard = KeyboardController()

a = "Work"
b = "Rest"

# ********** Part 1 **********

pyautogui.click(600, 1120)

keyboard.type(a)

keyboard.press(Key.enter)
keyboard.release(Key.enter)

sleep(2)

pyautogui.click(484, 1056)

sleep(2)

# ********** Part 2 **********

pyautogui.click(600, 1120)

keyboard.type(b)

keyboard.press(Key.enter)
keyboard.release(Key.enter)

sleep(2)

pyautogui.click(484, 1056)

sleep(30)

Actually Im Creating Like A Auto Msg Sender For Discord But I Want This Code Loops After 30 Seconds. I Have Used tkinter And Many More Things But It Didn't Helped Me Out. Pls Help Me Out With This Anyone. Thanks In Advance....

Titas
  • 1
  • 1
  • 2
    What have you tried? If I understand what you're asking, it's literally a while loop, the first thing that would have come up if you googled Python loop. Though note that using the keyboard and UI automation to send messages in Discord is pretty inefficient when discord offers an API. – Nick Bailey Jul 15 '22 at 22:27
  • Using Discord's bot API it not only cleaner and easier, it also doesn't violate the terms of use. – CrazyChucky Jul 15 '22 at 22:31
  • 1
    If you want this code in a loop, then ... put it in a loop. What is the difficulty? – John Gordon Jul 15 '22 at 22:34

1 Answers1

0

As others have commented, you could use the discord api.

Also I don't know why you use the keyboard, pyautogui has a press function. "press" is the same as pyautogui.keyDown(var) and pyautogui.keyUp(var) used in conjunction.

There's any number of ways to accomplish a timed loop.

For example a while loop.

time_to_sleep_in_seconds = 30
while True :
    #the rest of your code
    time.sleep(time_to_sleep_in_seconds)

Or you could wrap your code in a function and use a thread timer to allow your code to do other functions and run your function concurrently. (Or at least as concurrently as your interpreter allows for the threading module)

import threading

def your_function() :
    time_interval_in_seconds= 30
    threading.Timer(time_interval_in_seconds, your_function).start()
    #put your code here
FredMan
  • 861
  • 7
  • 19