基于PyQt5制作一个windows通知管理器
作者:Python 时间:2022-10-18 19:28:24
前几天看到一个python框架win10toast,它可以用来做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())
线程运行相关部分,通过继承 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
标签:PyQt5,windows,通知管理器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Yahoo!上的小秘密
2007-08-23 09:48:00
![](https://img.aspxhome.com/file/UploadPic/20078/23/200782395619331s.jpg)
详解Python中的type和object
2021-03-25 13:00:58
![](https://img.aspxhome.com/file/2023/1/67921_0s.png)
详解Python中的多线程编程
2023-09-17 00:34:08
![](https://img.aspxhome.com/file/2023/1/65131_0s.png)
Python实现鼠标自动在屏幕上随机移动功能
2022-01-30 05:08:15
SQL指令植入式攻击的危害及其防范措施
2008-12-19 14:07:00
《HTML5设计原理》读后随记
2011-01-25 12:26:00
Oracle关于时间/日期的操作
2009-02-26 10:37:00
用Python+OpenCV对比图像质量的几种方法
2022-06-28 10:57:59
![](https://img.aspxhome.com/file/2023/1/66821_0s.png)
如何基于pythonnet调用halcon脚本
2022-09-27 16:34:23
![](https://img.aspxhome.com/file/2023/2/67372_0s.png)
为什么str(float)在Python 3中比Python 2返回更多的数字
2022-11-09 22:55:56
不要忽略了颜色的可用性
2009-03-05 18:19:00
![](https://img.aspxhome.com/file/UploadPic/20093/5/01-24s.jpg)
ASP怎么读取指定xml 的节点
2008-04-28 13:12:00
如何实现某些页面只让特定的用户浏览?
2010-05-19 21:42:00
PHP 数组和字符串互相转换实现方法
2023-06-19 15:04:17
php设计模式 Singleton(单例模式)
2023-11-20 14:37:50
Asp中Server.ScriptTimeOut脚本超时属性需要注意的一点
2008-10-18 14:53:00
(100-1)% 的内容是导航
2008-01-11 19:23:00
Python爬虫之Selenium实现关闭浏览器
2022-12-09 07:11:03
用javascript实现的汉字简繁转换功能
2008-05-04 13:15:00
Python map及filter函数使用方法解析
2021-11-20 05:25:03