Hello,
I'm trying to make an alert GUI that pops up/activates after a certain amount of time or when it's called by my program.
Right now I'm trying to get it to be a reminder alert popup. In my program, I go into the terminal and type in "Remind me to do stuff", then type in a time when I want to be reminded. After the specified amount of time, I want the GUI to trigger.
This is what I have so far.
BX_External_Functions.py
(This is where the reminders get set)
(This is the Alert GUI)
Thanks in advance.
I'm trying to make an alert GUI that pops up/activates after a certain amount of time or when it's called by my program.
Right now I'm trying to get it to be a reminder alert popup. In my program, I go into the terminal and type in "Remind me to do stuff", then type in a time when I want to be reminded. After the specified amount of time, I want the GUI to trigger.
This is what I have so far.
BX_External_Functions.py
(This is where the reminders get set)
#-------------------------------------------------------------------------------------
# Reminder Function
#-------------------------------------------------------------------------------------
def setReminders(command):
#------------------------------------------
# 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) VALUES(?)",(command,))
connection.commit()
connection.close() #Close the connection
#------------------------------------------
#Ask whether the user wants to pick a time or have Baxter do it
results = "Would you like to pick a time for me to remind you, or should I do it?"
autoTypeAnimation(results)
speak(results)
#User enters choice and their input gets converted to lowercase
choiceCommand = UserInput() #Take user's input
choiceCommand=str(choiceCommand).lower() #Convert str cmd to lowercase
#Scan through the youChoose() list to see if the user's input matches
#If it does, have Baxter randomly choose a time to remind the user
#Else, have the user enter the time they would like to be reminded
patterns = youChoose()
if (choiceCommand in patterns):
#Baxter picks a random time to remind you
remindTimeHrs = random.uniform(0.5,5.0) #Range: 0.5hrs - 5hrs
remindTimeHrs = round(remindTimeHrs,2) #Round RemindTimeHrs to 2 decimal places
remindTimeSec = remindTimeHrs*3600 #Hrs->Sec
results = "Ok, I will remind you to" + command + " in " + str(remindTimeHrs) + " hours"
autoTypeAnimation(results)
speak(results)
t = threading.Timer(remindTimeSec, reminderPopup, args=(command,))
t.start() #Start the timer
else:
#User picks the time to be reminded
results = "When should I remind you to" + command +"?"
autoTypeAnimation(results)
speak(results)
while True:
autoTypeAnimation("Enter wait time (H:M:S) (Ex: 0:0:5 -> 5sec)")
timestr = UserInput()
if not timestr:
break
try:
h,m,s = timestr.split(':')
timestr = (int(datetime.timedelta(hours=int(h),minutes=int(m),seconds=int(s)).total_seconds()))
t = threading.Timer(timestr, reminderPopup, args=(command,))
results = "Ok, I will remind you to" + command + " in " + h + " Hours "+ m + " minutes " + s + " seconds"
autoTypeAnimation(results)
speak(results)
t.start() #Start the timer
break
except ValueError:
print("[green]Baxter: [red]Not a valid time input")
def reminderPopup(command):
#------------------------------------------
# Remove Reminder to Database
#------------------------------------------
#Connect to the database
connection = sqlite3.connect(MainDatabase)
cursor = connection.cursor()
#Add the reminder to the reminders table
cursor.execute("DELETE FROM Reminders WHERE Reminder = ?",(command,))
connection.commit()
connection.close() #Close the connection
#------------------------------------------
global reminderMsg
reminderMsg = command
#Have a GUI/MsgBox popup to display the reminder.
from BX_GUI_MsgAlert import main
main()
#-------------------------------------------------------------------------------------BX_GUI_MsgAlert.py(This is the Alert GUI)
#----------------------------------------------------------------------------------------------
# Table Of Contents/Overview
#----------------------------------------------------------------------------------------------
# Imports
# GUI
# - Msg Display
# -- Button Actions
# --- Button Functions
# ---- Retranslate UI
# Run Program
#----------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------------
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMainWindow)
from PyQt5.QtCore import Qt, QMetaObject, QCoreApplication
from PyQt5.QtGui import QFont
class Ui_MsgAlert(QMainWindow):
def __init__(self, parent = None):
super(Ui_MsgAlert, self).__init__(parent)
self.setObjectName("Alert")
self.setFixedSize(800, 250)
self.setStyleSheet("background-color: rgb(0, 0, 0);")
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.Header = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Robo Sapien")
font.setPointSize(25)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
self.Header.setFont(font)
self.Header.setStyleSheet("color: rgb(255, 0, 0);")
self.Header.setAlignment(QtCore.Qt.AlignCenter)
self.Header.setObjectName("Header")
self.verticalLayout.addWidget(self.Header)
self.Message = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Runlion")
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.Message.setFont(font)
self.Message.setStyleSheet("color: rgb(0, 255, 0);")
self.Message.setAlignment(QtCore.Qt.AlignCenter)
self.Message.setObjectName("Message")
self.verticalLayout.addWidget(self.Message)
self.AcknowledgeButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Robo Sapien")
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.AcknowledgeButton.setFont(font)
self.AcknowledgeButton.setStyleSheet("background-color: rgb(255, 170, 0);\n"
"color: rgb(0, 0, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
self.AcknowledgeButton.setObjectName("AcknowledgeButton")
self.verticalLayout.addWidget(self.AcknowledgeButton)
self.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
#----------------------------------------------------------------------------------------------------
# Msg Display
#----------------------------------------------------------------------------------------------------
Title = self.Header
Message = self.Message
from BX_External_Functions import reminderMsg
Title.setText("Reminder Alert!")
Message.setText("I was told to remind you to: " + reminderMsg)
#----------------------------------------------------------------------------------------------------
# Button Actions
#----------------------------------------------------------------------------------------------------
#------------------------------------------
# Acknowledge Button
#------------------------------------------
#When the Acknowledge button is clicked -> AcknowledgeClicked Function
AcknowledgeButton = self.AcknowledgeButton
AcknowledgeButton.clicked.connect(self.AcknowledgeClicked)
#------------------------------------------
#----------------------------------
# Acknowledge Function
#----------------------------------
def AcknowledgeClicked(self):
#Print in terminal for testing:
#print("Acknowledge Button Clicked")
self.close() #Quit this GUI
#----------------------------------
#----------------------------------------------------------------------------------------------------
# Retranslate Ui
#----------------------------------------------------------------------------------------------------
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "Alert"))
self.Header.setText(_translate("MainWindow", "Reminder Alert!"))
self.Message.setText(_translate("MainWindow", "I was told to remind you to:"))
self.AcknowledgeButton.setText(_translate("MainWindow", "Acknowledge"))
#----------------------------------------------------------------------------------------------------
# Run this Program
#----------------------------------------------------------------------------------------------------
def main():
app = QApplication(sys.argv)
win = Ui_MsgAlert()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
#----------------------------------------------------------------------------------------------------This works (The GUI pops up, I can hit the Acknowledge button & it will exit the GUI, and I can still go back to terminal and enter more commands) but I get this message in the terminal:Output:WARNING: QApplication was not created in the main() thread.So is there a way to do this properly so I don't get this message?Thanks in advance.
