3

大年初二、做了一个windows通知管理器!

 2 years ago
source link: https://segmentfault.com/a/1190000041362517
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

大年初二、做了一个windows通知管理器!

发布于 2 月 2 日

前几天看到一个python框架win10toast,它可以用来做windows的消息通知功能。通过设定通知的间隔时间来实现一些事件通知的功能,比如可以可以提醒一头扎进代码编写过程的我们按时喝水。

前往文末获取源代码 >>>

file

界面布局采用的依旧是pyqt5的ui设计,使用界面化直接设置好想要提示的内容和时间就可以给我们定时的发通知了。

UI相关的部分的还是这几个常用的组件包。

from PyQt5.QtGui import *  # UI 界面相关
from PyQt5.QtCore import *  # 核心组件包
from PyQt5.QtWidgets import *  # UI 布局相关模块

界面主题相关的模块,这里采用的是黑色的模块主题。

from qdarkstyle import load_stylesheet_pyqt5

应用相关的模块。

import sys
import os

下面几个模块中唯一比较特殊的就是win10toast模块是用来做windows通知的,还有一个用到了python线程中的定时器。

from win10toast import ToastNotifier  # 导入系统通知对象
import time  # 系统时间模块
import datetime
from threading import Timer  # 定时器

首先还是将UI界面中的布局和界面组件相关的部分写出来,界面也比较简单,采用了两种布局一种是Form表单布局、另外一个是垂直布局。

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

    def init_ui(self):
        self.setWindowTitle('windows通知管理器  公众号:[Python 集中营]')
        self.setWindowIcon(QIcon('通知.ico'))
        self.setFixedWidth(550)

        self.notify_subject_label = QLabel()
        self.notify_subject_label.setText('通知主题')

        self.notify_subject_text = QLineEdit()
        self.notify_subject_text.setPlaceholderText('输入通知主题')

        self.notify_current_label = QLabel()
        self.notify_current_label.setText('通知内容')

        self.notify_current_text = QLineEdit()
        self.notify_current_text.setPlaceholderText('输入通知内容')

        self.notify_time_label = QLabel()
        self.notify_time_label.setText('通知间隔')

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

        self.notify_icon_path = QLineEdit()
        self.notify_icon_path.setPlaceholderText('通知图标(*.ico)')

        self.notify_icon_btn = QPushButton()
        self.notify_icon_btn.setText('选择图标')
        self.notify_icon_btn.clicked.connect(self.notify_icon_btn_click)

        self.start_btn = QPushButton()
        self.start_btn.setText('开启通知吧!')
        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(), '打开图片', '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()

主函数启动应用时,将黑色主题加入到app的布局当中。

app.setStyleSheet(load_stylesheet_pyqt5())

file

线程运行相关部分,通过继承 QThead 类来编写子线程。

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('当前时间:%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()

公众号内回复"通知管理器"获取完整源代码 ......

我是 [Python 集中营]、很高兴您看到了最后, 我是一个专注于 Python 知识分享的公众号,希望可以得到您的关注~

file

【往期精彩】

gif动态图片生成器,多张图片组合后生成动图...

python几个常见的数据处理操作,一行代码就能完成!

过年了,用 PyQt5 生成一副春联吧...

记录一下python中的十大%占位符对应的格式化...

PyQt5 最小化到托盘,升级小闹钟...


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK