SHARE
TWEET

Untitled

a guest Mar 26th, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import pickle
  3. from datetime import datetime
  4.  
  5. from PyQt5.QtCore import Qt, QTimer, QDateTime
  6. from PyQt5.QtWidgets import *
  7. from win10toast import ToastNotifier
  8.  
  9. path = 'reminder.rmd'
  10.  
  11.  
  12. class Reminder(QListWidgetItem):
  13.     def __init__(self, text, datetime):
  14.         self.text = text
  15.         self.datetime = datetime
  16.         super(Reminder, self).__init__(self.__str__())
  17.  
  18.     def __str__(self):
  19.         return '{:<30}{:<50}'.format(self.datetime.strftime("%d.%m.%Y %H:%M:%S"), self.text)
  20.  
  21.     def getText(self):
  22.         return self.text
  23.  
  24.     def getDateTime(self):
  25.         return self.datetime
  26.  
  27.  
  28. def save_reminder(reminders):
  29.     pickleObj = [(reminder.text, reminder.datetime) for reminder in reminders]
  30.     with open(path, 'wb') as reminderFile:
  31.         pickle.dump(pickleObj, reminderFile)
  32.  
  33.  
  34. def load_reminders():
  35.     if os.path.isfile(path):
  36.         with open(path, 'rb') as reminderFile:
  37.             pickleObj = pickle.load(reminderFile)
  38.             return [Reminder(text, datetime) for text, datetime in pickleObj]
  39.     return []
  40.  
  41.  
  42. class MainWindow(QWidget):
  43.     def __init__(self, *args, **kwargs):
  44.         super(MainWindow, self).__init__(*args, **kwargs)
  45.  
  46.         self.setWindowTitle("RemindMe")
  47.         phText = 'Woran möchtest du erinnert werden?'
  48.         layoutHorTop = QHBoxLayout()
  49.         layoutHorBottom = QHBoxLayout()
  50.         layoutVer = QVBoxLayout()
  51.         inputText = QTextEdit()
  52.         inputText.setFixedSize(200, 50)
  53.         inputText.setPlaceholderText(phText)
  54.         checkboxDate = QCheckBox('Erinnern am:')
  55.         inputDate = QDateTimeEdit()
  56.         inputDate.setFixedSize(110, 25)
  57.         checkboxTime = QCheckBox('Erinnern in:')
  58.         inputTime = QTimeEdit()
  59.         inputTime.setFixedSize(80, 25)
  60.         inputTime.setDisplayFormat('hh:mm:ss')
  61.         buttonRemind = QPushButton('Erinnere mich!')
  62.         buttonRemind.setFixedSize(100, 30)
  63.         buttonReset = QPushButton('RESET')
  64.         buttonReset.setFixedSize(100, 30)
  65.  
  66.         self.listReminderView = QListWidget()
  67.         for reminder in load_reminders():
  68.             self.listReminderView.addItem(reminder)
  69.         layoutHorTop.addWidget(inputText)
  70.         layoutHorTop.addWidget(checkboxDate)
  71.         layoutHorTop.addWidget(inputDate)  # Kalender
  72.         layoutHorTop.addWidget(checkboxTime)
  73.         layoutHorTop.addWidget(inputTime)  # Timer
  74.         layoutVer.addLayout(layoutHorTop)  # Textbox und Dropdown
  75.         layoutVer.addWidget(self.listReminderView)
  76.         layoutVer.addLayout(layoutHorBottom)
  77.         layoutHorBottom.addWidget(buttonRemind)
  78.         layoutHorBottom.addWidget(buttonReset)
  79.  
  80.         self.setLayout(layoutVer)
  81.         self.trayIcon = QSystemTrayIcon(self)
  82.         self.trayIcon.setIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))
  83.  
  84.         show_action = QAction("Show", self)
  85.         show_action.triggered.connect(self.showWindow)
  86.         tray_menu = QMenu()
  87.         tray_menu.addAction(show_action)
  88.         self.trayIcon.setContextMenu(tray_menu)
  89.  
  90.         timer = QTimer(self)
  91.         timer.timeout.connect(self.timerEvent)
  92.         timer.start(1000)
  93.  
  94.         buttonReset.clicked.connect(self.on_button_reset_clicked)
  95.         buttonRemind.clicked.connect(
  96.             lambda: self.on_button_remind_clicked(inputText.toPlainText(), checkboxDate, checkboxTime, inputDate,
  97.                                                   inputTime))
  98.         checkboxDate.stateChanged.connect(lambda: self.on_check_state_changed(checkboxDate, checkboxTime))
  99.         checkboxTime.stateChanged.connect(lambda: self.on_check_state_changed(checkboxTime, checkboxDate))
  100.  
  101.     def timerEvent(self):
  102.         now = datetime.now()
  103.         for i in range(self.listReminderView.count()):
  104.             x = self.listReminderView.item(i)
  105.             if now > x.getDateTime():
  106.                 toaster = ToastNotifier()
  107.                 toaster.show_toast("RemindMe", x.getText(), duration=3, threaded=True)
  108.                 self.listReminderView.removeItemWidget(x)
  109.  
  110.     def on_button_reset_clicked(self):
  111.         self.listReminderView.clear()
  112.  
  113.     def eval_checkboxes(self, checkboxDate, checkboxTime, inputDate, inputTime):
  114.         if checkboxDate.isChecked():
  115.             return inputDate.dateTime().toPyDateTime()
  116.         elif checkboxTime.isChecked():
  117.             return QDateTime.currentDateTime().addSecs(inputTime.time().second() + inputTime.time().minute() * 60 + inputTime.time().hour() * 3600).toPyDateTime()
  118.  
  119.         else:
  120.             return None
  121.  
  122.     def on_check_state_changed(self, checkboxDate, checkboxTime):
  123.         if checkboxDate.isChecked() and checkboxTime.isChecked():
  124.             checkboxTime.setCheckState(Qt.Unchecked)
  125.  
  126.     def on_button_remind_clicked(self, remindText, checkboxDate, checkboxTime, inputDate, inputTime):
  127.         pointInTime = self.eval_checkboxes(checkboxDate, checkboxTime, inputDate, inputTime)
  128.         if pointInTime is not None:
  129.             reminder = Reminder(remindText, pointInTime)
  130.             self.listReminderView.addItem(reminder)
  131.         else:
  132.             return
  133.  
  134.     def closeEvent(self, event):
  135.         reminders = [self.listReminderView.item(i) for i in range(self.listReminderView.count())]
  136.         save_reminder(reminders)
  137.         event.accept()
  138.  
  139.     def hideEvent(self, hideEvent):
  140.         hideEvent.ignore()
  141.         self.hide()
  142.         self.trayIcon.show()
  143.  
  144.     def showWindow(self):
  145.         self.show()
  146.         self.setWindowState(Qt.WindowActive)
  147.         self.activateWindow()
  148.         self.trayIcon.hide()
  149.  
  150.  
  151. app = QApplication([])
  152. # app.setStyle('Windows')
  153.  
  154. window = MainWindow()
  155. window.show()
  156.  
  157. app.exec_()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top