基于PyQt5制作一個(gè)windows通知管理器
前幾天看到一個(gè)python框架win10toast,它可以用來做windows的消息通知功能。通過設(shè)定通知的間隔時(shí)間來實(shí)現(xiàn)一些事件通知的功能,比如可以可以提醒一頭扎進(jìn)代碼編寫過程的我們按時(shí)喝水。
界面布局采用的依舊是pyqt5的ui設(shè)計(jì),使用界面化直接設(shè)置好想要提示的內(nèi)容和時(shí)間就可以給我們定時(shí)的發(fā)通知了。
UI相關(guān)的部分的還是這幾個(gè)常用的組件包。
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
下面幾個(gè)模塊中唯一比較特殊的就是win10toast模塊是用來做windows通知的,還有一個(gè)用到了python線程中的定時(shí)器。
from win10toast import ToastNotifier # 導(dǎo)入系統(tǒng)通知對象 import time # 系統(tǒng)時(shí)間模塊 import datetime from threading import Timer # 定時(shí)器
首先還是將UI界面中的布局和界面組件相關(guān)的部分寫出來,界面也比較簡單,采用了兩種布局一種是Form表單布局、另外一個(gè)是垂直布局。
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ù)啟動(dòng)應(yīng)用時(shí),將黑色主題加入到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)前時(shí)間:%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制作一個(gè)windows通知管理器的文章就介紹到這了,更多相關(guān)PyQt5 windows通知管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Python使用Bottle來提供一個(gè)簡單的web服務(wù)
這篇文章主要介紹了淺談Python使用Bottle來提供一個(gè)簡單的web服務(wù),具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12Python實(shí)現(xiàn)GUI圖片瀏覽的小程序
這篇文章主要介紹了Python實(shí)現(xiàn)GUI圖片瀏覽程序,程序的實(shí)現(xiàn)需要pillow庫,pillow是 Python 的第三方圖像處理庫,需要安裝才能實(shí)用,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12python字符串駐留機(jī)制的使用范圍知識點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python字符串駐留機(jī)制的使用范圍相關(guān)知識點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-09-09Python2.7實(shí)現(xiàn)多進(jìn)程下開發(fā)多線程示例
這篇文章主要為大家詳細(xì)介紹了Python2.7實(shí)現(xiàn)多進(jìn)程下開發(fā)多線程示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05