网络编程
位置:首页>> 网络编程>> Python编程>> 基于PyQt5制作一个windows通知管理器

基于PyQt5制作一个windows通知管理器

作者:Python  发布时间:2022-10-18 19:28:24 

标签:PyQt5,windows,通知管理器

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

基于PyQt5制作一个windows通知管理器

界面布局采用的依旧是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())

基于PyQt5制作一个windows通知管理器

线程运行相关部分,通过继承 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()

来源:https://www.cnblogs.com/lwsbc/p/15860709.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com