所以我的應用程序有一個計時器,每隔幾分鐘就會提醒做幾秒鐘的事情。例如它倒計時 10 分鐘,然后在接下來的 10 秒內我將拉伸,計時器將計時 10 秒。當這 10 秒結束時,它會再次重置為 10 分鐘。因此,如果第一個計時器用完,我希望它說“是時候伸展了”并顯示一個 Windows Toast 通知。在 10 秒拉伸的第二個計時器結束后,我想顯示另一個通知,上面寫著“好的,你可以回去做你正在做的任何事情”。這是應用程序:這是代碼:from PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QApplication, QMainWindowfrom PyQt5 import QtCoreimport sysfrom win10toast import ToastNotifierimport itertoolsDURATION_INT = 10toaster = ToastNotifier()TIME_CYCLER = itertools.cycle([10, 5]) # 10 minutes, 10 secondsiterToast = 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 minsecclass 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)所以問題是這個cycle功能對我不起作用。每當我運行該應用程序時,它只顯示第一個test1通知,并在每次時鐘用完時重復。它甚至沒有通過第二個通知循環,所以我想itertools可能不是我要找的東西。任何幫助都會很棒:)
1 回答

蠱毒傳說
TA貢獻1895條經驗 獲得超3個贊
好的,所以我找到了解決方案。itertools我相信不適用于我的情況,但我在self.current_timer = 1我的類中創建了一個新變量并將此函數更改為如下所示:
def timerTimeout(self):
self.time_left_int -= 1
if self.time_left_int == 0:
if self.current_timer == 1:
toaster.show_toast("test1", "test1", duration=3, threaded=True)
self.current_timer = 2
elif self.current_timer == 2:
toaster.show_toast("test2", "test2", duration=3, threaded=True)
self.current_timer = 1
self.time_left_int = next(TIME_CYCLER)
self.update_gui()
所以最終,我手動將變量設置為每個通知所需的任何值。
添加回答
舉報
0/150
提交
取消