Reputation: 673
So my code opens a GUI timer that counts down. Looks like this:
So according to the code, the app will countdown initially from 15 seconds and when it reaches zero it will reset to 5 seconds, then 15 again. Ultimate goal is to remind me to do something for a few seconds, every few minutes. These initial times are just for the sake of testing.
So when the timer is zero, I'm waiting a few seconds before it resets to the inital int. In the meanwhile, I'm trying to show a window 10 toast notification but the issue is that when it appears, the clock is stuck and after that period the app closes. So basically I can see the toast for a few seconds, but the timer will not reset and the app will close.
Here is my code:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore
import sys
import time
from win10toast import ToastNotifier
import itertools
DURATION_INT = 15
toaster = ToastNotifier()
TIME_CYCLER = itertools.cycle([15, 5]) # 10 minutes, 10 seconds
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:
toaster.show_toast("test", "test", duration=5)
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_())
Maybe my logic is flawed with the def timerTimeout(self):
function, but from my understanding, the timer should reach zero, show the notification, reset self.time_left_int
to the next value, then call the self.update_gui()
function. In my case, it seems like the show_toast
is the last thing to happen.
Any help is appreciated :)
Upvotes: 1
Views: 1199
Reputation: 673
Looks like after further research, toaster has a threaded
parameter, so editing this in fixed the issue:
toaster.show_toast("test", "test", duration=5, threaded=True)`
Upvotes: 1