Python Forum
Making certain texts stop at a certain time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making certain texts stop at a certain time
#1
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
Yoriz write May-20-2021, 04:25 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
introduce counter for each message and if message reach limit then stop with this message.
Reply
#3
(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
Reply
#4
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(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
Reply
#6
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
this giving me some ideas i think , you were golden thanks a lot this is gonna help me
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  comparision of texts in a file saisankalpj 1 751 Dec-22-2022, 03:11 PM
Last Post: Larz60+
  Time.sleep: stop appending item to the list if time is early quest 0 1,847 Apr-13-2021, 11:44 AM
Last Post: quest
  [ library recommendation ] search & highlight target texts in PDF? smallabc 1 2,193 Nov-27-2019, 10:40 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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