首页 > 解决方案 > 使用 ToastNotifier (win10toast) 后程序的窗口关闭

问题描述

最近我遇到了这个问题。有提醒在某个时间段内通知某事。我使用 Python 3.7 和 PyQt5 来创建界面。此外,还有一个名为“win10toast”的库和用于通知的模块“ToastNotifer”。输入数据(提醒的时间和文本)并单击提交按钮后,窗口会关闭,但有时窗口的标题会更改为“程序不应答”,并且在 2-3 秒后窗口也会关闭。我认为,它与win10toast有关。我需要关闭程序,因为使用提醒后,用户可以返回并打开记事本或只是在那里写下他的|她的标记。那是我无法解决的问题。我不知道为什么会这样。Reminder 的代码从类 fileush 开始直到结束片段。你必须从我的 github 下载 zip 文件,因为有主要的python和ui(界面)程序。请安装库 win10toast 并同时导入 ToastNotifier,datetime 以了解当前时间。谢谢)我的github链接在问题的最后

我的代码:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPlainTextEdit
from PyQt5.QtWidgets import QLabel, QPushButton, QMessageBox, QFileDialog
from PyQt5.QtWidgets import QTimeEdit
from PyQt5.QtGui import QPixmap
from win10toast import ToastNotifier
import time
from datetime import datetime

class fileush(QWidget):
    def __init__(self, *args):
        super().__init__()

        uic.loadUi('ui_fileush.ui', self)
        self.pushButton.clicked.connect(self.running)

    def running(self):
        toaster = ToastNotifier()
        self.now = datetime.now()
        self.tmf = self.tm.time()
        self.uak = self.tmf.toString()
        self.current_time = self.now.strftime("%H:%M:%S")
        self.seku = self.current_time.split(":")
        self.seku2 = self.uak.split(":")
        self.seknot = 3600 * (int(self.seku2[0])) + (int(self.seku2[1])) * 60
        self.sekcur = 3600 * (int(self.seku[0])) + (int(self.seku[1])) * 60 + (int(self.seku[2]))

        if self.sekcur >= self.seknot:
            self.label_5.setText('ERROR')

        else:
            self.label_5.setText('')
            self.secs = 3600 * (int(self.seku2[0]) - int(self.seku[0])) + 60 * (int(self.seku2[1]) - int(self.seku[1])) - int(self.seku[2])
            self.second_input = self.lineEdit_2.text()
            t = time.sleep(int(self.secs))
            toaster.show_toast(self.second_input)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = fileush()
    ex.show()
    sys.exit(app.exec())

链接到我的github:https ://github.com/iMAGA07/reminder

标签: pythondatetimetimepyqt5notify

解决方案


我有同样的问题,但我用一个线程来解决它。

如果您不熟悉线程,它们基本上可以帮助您同时运行不同的功能,我写的代码是:

import threading

thread = threading.Thread(target=self.noti)
thread.start()

def noti(self):
    h = ToastNotifier()
    h.show_toast("TODO UPDATE", f"Hurry up, you have less than 3 days to finish the task you had set {task}!!", )

在这里,我试图制作一个待办事项应用程序,当完成他们设定的任务不到 3 天时,它会发出通知。


推荐阅读