Thanks for your help. I finally got it to work properly!
I scheduled a couple test reminders and they all popped up at their scheduled time.
This is my code (I used npyscreen as a GUI to set my reminders):
How would I go about deleting old reminders?
Tried the code below but I get this error:
I scheduled a couple test reminders and they all popped up at their scheduled time.
Output:#New Reminder I scheduled for 1:00 PM
01:00 PM
2022-10-16 13:00:00
Reminders Popup: 2022-10-16 12:46:00: Test 4
Reminders Popup: 2022-10-16 12:46:00: T
Reminders Popup: 2022-10-16 12:52:00: Test 4
Reminders Popup: 2022-10-16 12:52:00: Test 5
Reminders Popup: 2022-10-16 12:54:00: Test 6
Reminders Popup: 2022-10-16 12:54:00: Test 7
Reminders Popup: 2022-10-16 12:59:00: Test C
Reminders Popup: 2022-10-16 13:00:00: Test B
Reminders Popup: 2022-10-16 13:00:00: Test no
Reminders Popup: 2022-10-16 13:00:00: Test 8
Reminders Popup: 2022-10-16 13:00:00: Test 3
Reminders Popup: 2022-10-16 13:00:00: Test A
Reminders Popup: 2022-10-16 13:00:00: Test 1
Reminders Popup: 2022-10-16 13:00:00: Test D
Reminders Popup: 2022-10-16 13:00:00: Test 9
Now all that's left to do is delete the old reminders, clean up the code, and test it/tie it in to my main code. This is my code (I used npyscreen as a GUI to set my reminders):
# Working Test Version import npyscreen import sqlite3 from BX_Constants import (MainDatabase) import sched import time from datetime import datetime #Create schedule scheduler = sched.scheduler(time.time, time.sleep) class SetReminderTUI(npyscreen.NPSApp): def main(self): frame = npyscreen.Form(name = "Set Reminder",) Reminder = frame.add(npyscreen.TitleText, name = "Reminder:",) date = frame.add(npyscreen.TitleDateCombo, name = "Date:") AmPm = frame.add(npyscreen.TitleText, name = "AM/PM:",) time = frame.add(npyscreen.TitleSelectOne, max_height=5, value = [1,], name="Choose a Time", values = ["12:00","12:30","12:59","01:00","01:30" ,"02:00","02:30","03:00","03:30","04:00","04:30","05:00" ,"05:30","06:00","06:30","07:00","07:30" ,"08:00","08:30", "09:00","09:30","10:00","10:30" ,"11:00","11:30"], scroll_exit=True) # This lets the user interact with the Form. frame.edit() Reminder = Reminder.value SelectedTime = time.get_selected_objects() AmPm = AmPm.value Date = date.value #Convert SelectedTime from Tuple to List from itertools import chain SelectedTimeLst = list(chain.from_iterable(SelectedTime)) #Then Convert the List to a String SelectedTimeStr = "".join(str(x) for x in SelectedTimeLst) Time = SelectedTimeStr +" "+ AmPm from datetime import datetime dateTime = datetime.strptime(f"{Date} {Time}", "%Y-%m-%d %I:%M %p") print(Time) print(dateTime) #Save the Datetime as a timestamp in the database Scheduled_Time = dateTime.timestamp() #------------------------------------------ # Add Reminder to Database #------------------------------------------ #Connect to the database connection = sqlite3.connect(MainDatabase) cursor = connection.cursor() #Add the reminder to the reminders table cursor.execute("INSERT into Reminders (Reminder, DateTime) VALUES(?,?)",(Reminder,Scheduled_Time)) connection.commit() connection.close() #Close the connection #------------------------------------------ App = SetReminderTUI() App.run() def schedule_reminder(schedule): """Schedule a reminder""" connection = sqlite3.connect(MainDatabase) for reminder in connection.execute("SELECT Reminder, DateTime FROM reminders"): message, sched_time = reminder scheduler.enterabs(sched_time, 1, send_reminder, (message, sched_time)) connection.close() def send_reminder(Reminder, scheduled_time): """Function called when time to send reminder""" scheduled_timeStr = datetime .fromtimestamp(scheduled_time) print(f"Reminders Popup: {scheduled_timeStr}: {Reminder}") schedule_reminder(scheduler) while not scheduler.empty(): scheduler.run(False)Edit:
How would I go about deleting old reminders?
Tried the code below but I get this error:
Error: connection.execute("DELETE FROM reminders WHERE DateTimeStamp = ?",(scheduled_time))
ValueError: parameters are of unsupported type
Even through my DateTimeStamp is stored as a REAL value (Float) in my Database, and scheduled_time is also a float#Display the Reminder def send_reminder(Reminder, scheduled_time): """Function called when time to send reminder""" #Convert the scheduled_time.timestamp() to a readable DateTime String scheduled_timeStr = datetime .fromtimestamp(scheduled_time) reminderPopup(Reminder) print(f"Reminders Popup: {scheduled_timeStr}: {Reminder}") #Delete reminder after it's been displayed connection = sqlite3.connect(MainDatabase) #Connect to Database connection.execute("DELETE FROM reminders WHERE DateTimeStamp = ?",(scheduled_time)) connection.close()