On the second day of the lunar new year, I made a windows notification manager

Posted by Quest on Thu, 03 Feb 2022 00:11:13 +0100

A few days ago, I saw a python framework win10toast, which can be used to do the message notification function of windows. Some event notification functions can be realized by setting the notification interval. For example, it can remind us to drink water on time.

Go to the end of the article to get the source code > > >

The interface layout still adopts pyqt5's ui design. Using the interface, you can directly set the content and time you want to prompt, and then you can send us regular notices.

The UI related parts are still these common component packages.

from PyQt5.QtGui import *  # UI interface related
from PyQt5.QtCore import *  # Core component package
from PyQt5.QtWidgets import *  # UI layout related modules

The module related to the interface theme adopts the black module theme here.

from qdarkstyle import load_stylesheet_pyqt5

Application related modules.

import sys
import os

The only special module in the following modules is the win10toast module, which is used for windows notification, and a timer in python thread.

from win10toast import ToastNotifier  # Import system notification object
import time  # System time module
import datetime
from threading import Timer  # timer

First, write out the layout of the UI interface and the relevant parts of the interface components. The interface is also relatively simple. Two layouts are adopted, one is Form layout and the other is vertical layout.

class WinNotify(QWidget):
    def __init__(self):
        super(WinNotify, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('windows The official account number of the notification manager:[Python concentration camp]')
        self.setWindowIcon(QIcon('notice.ico'))
        self.setFixedWidth(550)

        self.notify_subject_label = QLabel()
        self.notify_subject_label.setText('Notification subject')

        self.notify_subject_text = QLineEdit()
        self.notify_subject_text.setPlaceholderText('Enter notification subject')

        self.notify_current_label = QLabel()
        self.notify_current_label.setText('Notice content')

        self.notify_current_text = QLineEdit()
        self.notify_current_text.setPlaceholderText('Enter notification content')

        self.notify_time_label = QLabel()
        self.notify_time_label.setText('Notification interval')

        self.notify_time_combox = QComboBox()
        self.notify_time_combox.addItems(['10|minute', '30|minute', '45|minute', '60|minute', '120|minute'])

        self.notify_icon_path = QLineEdit()
        self.notify_icon_path.setPlaceholderText('Notification icon(*.ico)')

        self.notify_icon_btn = QPushButton()
        self.notify_icon_btn.setText('Select Icon')
        self.notify_icon_btn.clicked.connect(self.notify_icon_btn_click)

        self.start_btn = QPushButton()
        self.start_btn.setText('Open the notice!')
        self.start_btn.clicked.connect(self.start_btn_click)

        form = QFormLayout()
        form.addRow(self.notify_subject_label, self.notify_subject_text)
        form.addRow(self.notify_current_label, self.notify_current_text)
        form.addRow(self.notify_time_label, self.notify_time_combox)
        form.addRow(self.notify_icon_path, self.notify_icon_btn)

        vbox = QVBoxLayout()
        vbox.addLayout(form)
        vbox.addWidget(self.start_btn)

        self.thread_ = WorkThread(self)

        self.setLayout(vbox)

    def notify_icon_btn_click(self):
        file = QFileDialog.getOpenFileName(self, os.getcwd(), 'Open picture', 'ICO File(*.ico)')
        print(file[0])
        self.notify_icon_path.setText(file[0])

    def start_btn_click(self):
        self.start_btn.setEnabled(False)
        self.thread_.start()

When the main function starts the application, the black theme is added to the layout of the app.

app.setStyleSheet(load_stylesheet_pyqt5())

The thread runs the relevant part and writes the sub thread by inheriting the QThead class.

class WorkThread(QThread):

    def __init__(self,parent=None):
        super(WorkThread, self).__init__(parent)
        self.parent = parent
        self.notify = ToastNotifier()
        self.working = True

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        self.show_toast()

    def show_toast(self):
        notify_head = self.parent.notify_subject_text.text()
        notify_text = self.parent.notify_current_text.text()
        notify_ico = self.parent.notify_icon_path.text()
        notify_sen = self.parent.notify_time_combox.currentText().split('|')[0]
        notify_sen = int(notify_sen) * 60
        print('current time :%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
        self.notify.show_toast(f"{notify_head}", f"{notify_text}", duration=5, threaded=True, icon_path=notify_ico)
        while self.notify.notification_active():
            time.sleep(0.005)
        timer = Timer(notify_sen, self.show_toast)
        timer.start()

The official account is returned to the notification manager to get the complete source code.

I am a [Python camp. I am glad you saw it. Finally, I am a official account focused on Python knowledge sharing. I hope you can get your attention.

[previous highlights]

gif dynamic picture generator, multiple pictures are combined to generate dynamic pictures

python several common data processing operations, one line of code can be completed!

Chinese new year, use PyQt5 to generate a pair of Spring Festival couplets

Record the formatting corresponding to the top ten% placeholders in python

Minimize PyQt5 to tray, upgrade small alarm clock

Topics: Data Analysis