大年初二、做了一个windows通知管理器!
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.
大年初二、做了一个windows通知管理器!
前几天看到一个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()
公众号内回复"通知管理器"获取完整源代码 ......
我是 [Python 集中营]、很高兴您看到了最后, 我是一个专注于 Python 知识分享的公众号,希望可以得到您的关注~
【往期精彩】
gif动态图片生成器,多张图片组合后生成动图...
python几个常见的数据处理操作,一行代码就能完成!
过年了,用 PyQt5 生成一副春联吧...
记录一下python中的十大%占位符对应的格式化...
PyQt5 最小化到托盘,升级小闹钟...
Recommend
-
36
2月7日,有网友在社交网络上晒出的图片显示,大年初二当天(2月6日)现年74周岁任正非亲自去看望了华为一线值班员工,并与员工握手拜年、合影留念。因为华为全球员工有18万人,很多员工都没有见过任正非本人,很多员工一边与任正非握手,一边称“老板过年好”,一边...
-
35
问与答 - @a5b6c7d8 - 防止自己看信谣传谣!好像我做错了什么
-
13
分享创造 - @little_cup - 大家好,我是曾开发过 [剪纸堆 Clip Stack]( https://play.google.com/store/apps/details?id=com.catchingnow.tinyclipb
-
20
最近有位日本女网友在推特分享自己的电脑桌摆设,包括双屏幕、键盘、鼠标还有麦克风等等周边设备,看起来是个十足的电脑游戏迷。然而就因为她自称还在就读初中2年级,因此遭到大量网友议论而走红,声称她拿父母的钱在炫耀,还有就的网友挑明自己就是羡慕又忌妒,看...
-
8
大年初二,50000 美元的 BTC 什么时候能到必读:预测行情都是假的,鬼知道下一秒有没任性的庄家来个拉升砸盘啥的。我倡导的是科学的投资理念,所有的分析都建立在科学的基础上,杜绝人为幻想。投资首先要做风险控制,别期望每次操作你都能赚钱。...
-
3
初二的孩子晚上做作业非要一边听音乐一边做,影响学习吗?初二的孩子不好沟通,如果强行不让他听又不好,该如何去教育?229
-
1
大年初二单日总票房突破10亿元-商业-金融界 大年初二单日总票房突破10亿元
-
10
大年初二,新春祝福福利红包 581 14764 只看楼主...
-
4
不仅大年初二,我预计2022年春节档总票房和出票数相较2021年春节档都要大幅下滑。说单日,容易被辩称“单日波动大”“春节档期不止一天”。那么看看可以占到春节档总票房大半江山的大年初一至初三的票房,够有说服力了吧?以...
-
4
【春节回家】大年初二游蒙自老海关_云南论坛_太平洋汽车网论坛...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK