基于PyQt5制作一個windows通知管理器
前幾天看到一個python框架win10toast,它可以用來做windows的消息通知功能。通過設(shè)定通知的間隔時間來實(shí)現(xiàn)一些事件通知的功能,比如可以可以提醒一頭扎進(jìn)代碼編寫過程的我們按時喝水。

界面布局采用的依舊是pyqt5的ui設(shè)計,使用界面化直接設(shè)置好想要提示的內(nèi)容和時間就可以給我們定時的發(fā)通知了。
UI相關(guān)的部分的還是這幾個常用的組件包。
from PyQt5.QtGui import * # UI 界面相關(guān) from PyQt5.QtCore import * # 核心組件包 from PyQt5.QtWidgets import * # UI 布局相關(guān)模塊
界面主題相關(guān)的模塊,這里采用的是黑色的模塊主題。
from qdarkstyle import load_stylesheet_pyqt5
應(yīng)用相關(guān)的模塊。
import sys import os
下面幾個模塊中唯一比較特殊的就是win10toast模塊是用來做windows通知的,還有一個用到了python線程中的定時器。
from win10toast import ToastNotifier # 導(dǎo)入系統(tǒng)通知對象 import time # 系統(tǒng)時間模塊 import datetime from threading import Timer # 定時器
首先還是將UI界面中的布局和界面組件相關(guān)的部分寫出來,界面也比較簡單,采用了兩種布局一種是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('通知內(nèi)容')
self.notify_current_text = QLineEdit()
self.notify_current_text.setPlaceholderText('輸入通知內(nèi)容')
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('通知圖標(biāo)(*.ico)')
self.notify_icon_btn = QPushButton()
self.notify_icon_btn.setText('選擇圖標(biāo)')
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()
主函數(shù)啟動應(yīng)用時,將黑色主題加入到app的布局當(dāng)中。
app.setStyleSheet(load_stylesheet_pyqt5())

線程運(yùn)行相關(guān)部分,通過繼承 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('當(dāng)前時間:%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()到此這篇關(guān)于基于PyQt5制作一個windows通知管理器的文章就介紹到這了,更多相關(guān)PyQt5 windows通知管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用UDP實(shí)現(xiàn)客戶端和服務(wù)器對話
這篇文章主要為大家介紹了python使用UDP實(shí)現(xiàn)客戶端和服務(wù)器對話示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
將tensorflow的ckpt模型存儲為npy的實(shí)例
今天小編就為大家分享一篇將tensorflow的ckpt模型存儲為npy的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Opencv中的cv2.calcHist()函數(shù)的作用及返回值說明
這篇文章主要介紹了Opencv中的cv2.calcHist()函數(shù)的作用及返回值說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
python使用itchat實(shí)現(xiàn)手機(jī)控制電腦
這篇文章主要為大家詳細(xì)介紹了python使用itchat實(shí)現(xiàn)手機(jī)控制電腦,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

