0

I'm trying to make a notifier at an exact time Here's the code:

from win10toast import ToastNotifier
from datetime import datetime
now = datetime.now()

toaster = ToastNotifier()

while True:
    if now.strftime('%I:%M %p') == "1:23 PM":
        toaster.show_toast("Bot:","It's 1:23 PM")
    elif now.strftime('%I:%M %p') == "4:43 PM":
        toaster.show_toast("Bot:","It's 4:43 PM")
    elif now.strftime('%I:%M %p') == "7:32 PM":
        toaster.show_toast("Bot:","It's 7:32 PM")
    elif now.strftime('%I:%M %p') == "8:44 PM":
        toaster.show_toast("Bot:","It's 8:44 PM")
    elif now.strftime('%I:%M %p') == "6:02 AM":
        toaster.show_toast("Bot:","It's 6:02 AM")

But when I run the code yesterday, the notification shows forever, and when I close it, it shows again. Please help me fix this and thank you!

Blue Duck
  • 56
  • 7
  • If you want to execute the ```if...elif...else``` only once, why use a ```while``` loop? You can add ```break``` after ```if``` statement –  Aug 05 '21 at 03:42
  • what is the purpose of the while loop? – joesitton Aug 05 '21 at 03:46
  • I want it to be automatic, I don't want to run the code at the specific time, I want to run the code once, and the rest will be automatic – Blue Duck Aug 05 '21 at 03:50

4 Answers4

1

You can add break at the end of your if-block

while True:
    if now.strftime('%I:%M %p') == "1:23 PM":
        toaster.show_toast("Bot:","It's 1:23 PM")
        break
    elif now.strftime('%I:%M %p') == "4:43 PM":
        toaster.show_toast("Bot:","It's 4:43 PM")
        break
    elif now.strftime('%I:%M %p') == "7:32 PM":
        toaster.show_toast("Bot:","It's 7:32 PM")
        break
    elif now.strftime('%I:%M %p') == "8:44 PM":
        toaster.show_toast("Bot:","It's 8:44 PM")
        break
    elif now.strftime('%I:%M %p') == "6:02 AM":
        toaster.show_toast("Bot:","It's 6:02 AM")
        break
  • wouldn't it stop my while loop from running? – Blue Duck Aug 05 '21 at 03:44
  • @BlueDuck. Yes, it would stop –  Aug 05 '21 at 03:45
  • Well I want it to continue so when it's 4 pm it shows a notification again. – Blue Duck Aug 05 '21 at 03:47
  • @BlueDuck - Don't you want your while-loop to stop after it matches an if/else statement? Also, `now = datetime.now()` should be inside the `while` if you want to re-evaluate it each time. – humaira.anjumi Aug 05 '21 at 03:49
  • I wanted to make it like when it's 10 am it shows notification once and then the script waits until it's 11 am and shows notification again and then wait until it's 12 am and run the code again, I want this script to be looped forever – Blue Duck Aug 05 '21 at 04:00
1

I think if you want to run only once, you don't needs to use loop want to check state using conditional statement or add break after if state

from win10toast import ToastNotifier
from datetime import datetime

now = datetime.now()

toaster = ToastNotifier()

if now.strftime('%I:%M %p') == "1:23 PM":
    toaster.show_toast("Bot:", "It's 1:23 PM")
elif now.strftime('%I:%M %p') == "4:43 PM":
    toaster.show_toast("Bot:", "It's 4:43 PM")
elif now.strftime('%I:%M %p') == "7:32 PM":
    toaster.show_toast("Bot:", "It's 7:32 PM")
elif now.strftime('%I:%M %p') == "8:44 PM":
    toaster.show_toast("Bot:", "It's 8:44 PM")
elif now.strftime('%I:%M %p') == "6:02 AM":
    toaster.show_toast("Bot:", "It's 6:02 AM")
Seonghun
  • 161
  • 7
  • but that wouldn't work, it will only work when I run the code at the specific time. I want it to be automatic and I need to run the code once not running the code when it's 1:23 pm and so on – Blue Duck Aug 05 '21 at 03:49
  • 1
    ahh, check this schedule library https://pypi.org/project/schedule/ it makes easy to run automatically at time you want – Seonghun Aug 05 '21 at 03:54
  • Can you code it out for me, I don't know how to use it @Seonghun – Blue Duck Aug 05 '21 at 04:03
1
import schedule
import time
from win10toast import ToastNotifier
from datetime import datetime

now = datetime.now()
toaster = ToastNotifier()

def job():
    toaster.show_toast("Bot: It's 10:30 PM")

schedule.every().day.at("10:30").do(job) #change time 10:30 to you want

while True:
    schedule.run_pending()
    time.sleep(1)
Seonghun
  • 161
  • 7
0

For that end you would be better using the schedule package! Refer to this answer Schedule runs like a clock timer (cron) you set the alarm time and it runs following the configuration... You could configure it to run everyday at a specific time or multiple times.

import schedule
import time

def job():
    print("I'm working...")

schedule.every().day.at("10:30").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
Alfredo Neto
  • 24
  • 1
  • 5
  • I don't know how to use this library, can you code it out for me please? I'm so sorry. – Blue Duck Aug 05 '21 at 03:58
  • Improved the answer with the code example: this now run the job every day at 10:30... You can change the time to whatever you want. And edit the job to run what you want at the time specified. – Alfredo Neto Aug 06 '21 at 05:22