jithurjacob / Windows-10-Toast-Notifications

Python library to display Windows 10 Toast Notifications
MIT License
964 stars 168 forks source link

Using two different notifications with itertools? #72

Closed Beinish closed 4 years ago

Beinish commented 4 years ago

Hello,

So my app is a timer that counts down from X to zero, then resets to Y and then counts down to zero again, and then it becomes X again. So generally it reminds me to do something for a few seconds, every few minutes. I'd like to toast notifications to do the same.

So if the first timer runs out, I'd like it to say "it's time to do that thing for a few seconds". When the timer runs out, it should say "ok, times up, you can get back to doing whatever you were doing". For the sake of testing, I've just written "test1" and "test2" to see when each notification appears.

It seems like I'm only able to show the first notification because I'm not sure how to place the second one inside the code.

Here is the code:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore
import sys
from win10toast import ToastNotifier
import itertools

DURATION_INT = 10
toaster = ToastNotifier()
TIME_CYCLER = itertools.cycle([10, 5])  # 10 minutes, 10 seconds
iterToast = itertools.cycle([toaster.show_toast("test1", "test1", duration=3, threaded=True), toaster.show_toast("test2", "test2", duration=3, threaded=True)])

def secs_to_minsec(secs: int):
    mins = secs // 60
    secs = secs % 60
    minsec = f'{mins:02}:{secs:02}'
    return minsec

class App(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.time_left_int = DURATION_INT
        self.myTimer = QtCore.QTimer(self)

        # App window
        self.app = QApplication(sys.argv)
        self.win = QMainWindow()
        self.win.setGeometry(200, 200, 200, 200)
        self.win.setWindowTitle("test")

        # Widgets
        self.titleLabel = QtWidgets.QLabel(self.win)
        self.titleLabel.setText("Welcome to my app")
        self.titleLabel.move(50,20)

        self.timerLabel = QtWidgets.QLabel(self.win)
        self.timerLabel.move(50,50)
        self.timerLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.timerLabel.setStyleSheet("font: 10pt Helvetica")

        self.startButton = QtWidgets.QPushButton(self.win)
        self.startButton.setText("Start")
        self.startButton.move(50,100)
        self.startButton.clicked.connect(self.startTimer)

        self.stopButton = QtWidgets.QPushButton(self.win)
        self.stopButton.setText("Minimize")
        self.stopButton.move(50,130)

        self.update_gui()

        # Show window
        self.win.show()
        sys.exit(app.exec_())

    def startTimer(self):
        self.time_left_int = next(TIME_CYCLER)
        self.myTimer.timeout.connect(self.timerTimeout)
        self.myTimer.start(1000)

    def timerTimeout(self):
        self.time_left_int -= 1
        if self.time_left_int == 0:
            next(iterToast)
            # toaster.show_toast("test1", "test1", duration=3, threaded=True)
            self.time_left_int = next(TIME_CYCLER)

        self.update_gui()

    def update_gui(self):
        minsec = secs_to_minsec(self.time_left_int)
        self.timerLabel.setText(minsec)

    # def minimize():
    #     pass

app = QtWidgets.QApplication(sys.argv)
main_window = App()
main_window.show()
sys.exit(app.exec_())

After 10 seconds, the next time that will appear will be of 5 seconds. When the 10 seconds timer is up, it shows the first notification test1. I want to add the test2 notification to the end of the second timer. I tried using itertools cycle and have each parameter just be a different toast notification, but as soon as I run the app, the notification appears, even though I'm not calling it yet.

Any help would be great :)