Python Forum

Full Version: Making certain texts stop at a certain time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello , i'm trying to create a loop with 3 text messages . All of them run in the same time using just a time.sleep to create a pause but i don't know how to make them stop in a given order smth like text1 stop after 10 seconds text 2 stop at 15 and text 3 after 5 , i managed to get this right just for 1 text but don't know how to do it to a number of different texts
import time
import pyautogui,datetime

start = time.time()

PERIOD_OF_TIME = 10 # 10secs
while True :
    print(datetime.datetime.now())
    pyautogui.typewrite("Text 1")
    pyautogui.press("enter")
    time.sleep(5)

    if time.time() > start + PERIOD_OF_TIME : break
introduce counter for each message and if message reach limit then stop with this message.
(May-20-2021, 03:13 PM)kashcode Wrote: [ -> ]introduce counter for each message and if message reach limit then stop with this message.
Thank you , i'll try Big Grin
This might help get you started
from time import sleep, localtime
texts = {5:'text 1', 10:'text 2', 15:'text 3'}
start_time = localtime().tm_sec
counter = 0
while counter < 20:
    counter += 1
    sleep(1)
    elapsed_time = localtime().tm_sec - start_time
    if elapsed_time in texts.keys():
        print(f'Elapsed Time: {elapsed_time} - {texts[elapsed_time]}')
Output:
Elapsed Time: 5 - text 1 Elapsed Time: 10 - text 2 Elapsed Time: 15 - text 3
(May-20-2021, 06:21 PM)menator01 Wrote: [ -> ]This might help get you started
from time import sleep, localtime
texts = {5:'text 1', 10:'text 2', 15:'text 3'}
start_time = localtime().tm_sec
counter = 0
while counter < 20:
    counter += 1
    sleep(1)
    elapsed_time = localtime().tm_sec - start_time
    if elapsed_time in texts.keys():
        print(f'Elapsed Time: {elapsed_time} - {texts[elapsed_time]}')
Output:
Elapsed Time: 5 - text 1 Elapsed Time: 10 - text 2 Elapsed Time: 15 - text 3
thanks a lot , now i just need to see where to put the pyautogui comand , a much better starting that what i had haha
I've not worked with pyautogui so, can't help with that but, as a side note you can also have multiple text for the timers.

from time import sleep, localtime
from random import choice

start_time = localtime().tm_sec
counter = 0
multi_text = ['text 1', 'text 4', 'text 5', 'text 6']
while counter < 120:
    texts = {5:choice(multi_text), 10:'text 2', 15:'text 3'}
    counter += 1
    sleep(1)
    elapsed_time = localtime().tm_sec - start_time
    if elapsed_time in texts.keys():
        print(f'Elapsed Time: {elapsed_time} - {texts[elapsed_time]}')
Output:
Elapsed Time: 5 - text 1 # First output Elapsed Time: 10 - text 2 Elapsed Time: 15 - text 3 Elapsed Time: 5 - text 6 # Second output Elapsed Time: 10 - text 2 Elapsed Time: 15 - text 3
this giving me some ideas i think , you were golden thanks a lot this is gonna help me