溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于PyQt5怎么制作一個windows通知管理器

發(fā)布時間:2022-02-07 09:51:13 來源:億速云 閱讀:119 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“基于PyQt5怎么制作一個windows通知管理器”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

python框架win10toast,它可以用來做windows的消息通知功能。通過設(shè)定通知的間隔時間來實現(xiàn)一些事件通知的功能,比如可以可以提醒一頭扎進代碼編寫過程的我們按時喝水。

基于PyQt5怎么制作一個windows通知管理器

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

應用相關(guān)的模塊。

import sys
import os

下面幾個模塊中唯一比較特殊的就是win10toast模塊是用來做windows通知的,還有一個用到了python線程中的定時器。

from win10toast import ToastNotifier  # 導入系統(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('通知圖標(*.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()

主函數(shù)啟動應用時,將黑色主題加入到app的布局當中。

app.setStyleSheet(load_stylesheet_pyqt5())

基于PyQt5怎么制作一個windows通知管理器

線程運行相關(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('當前時間:%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()

“基于PyQt5怎么制作一個windows通知管理器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI