利用Python做一個電腦通知小工具
序言
Windows不是有個消息通知功能,挺喜歡這個功能的,但是不太方便使用,也懶得去研究,于是準(zhǔn)備用Python自己寫一個,通過設(shè)定通知的間隔時間來實(shí)現(xiàn)類似鬧鐘的效果,這樣既不用聽鬧鐘的吵鬧聲,又做到了定時通知的效果,比如定時通知埋頭寫代碼的我們按時喝水。
說干就干,直接使用pyqt來設(shè)計(jì)成人人都可以用的工具。
效果展示



代碼實(shí)戰(zhàn)
UI部分使用的包
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 # 定時器
主要代碼
class WinNotify(QWidget):
def __init__(self):
super(WinNotify, self).__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('windows通知管理器 源碼自取君羊708525271')
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)部分
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)于利用Python做一個電腦通知小工具的文章就介紹到這了,更多相關(guān)Python電腦通知工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?Pygame實(shí)戰(zhàn)之打磚塊游戲的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)經(jīng)典的游戲—打磚塊。玩家操作一根螢?zāi)簧纤降摹鞍糇印保屢活w不斷彈來彈去的“球”在撞擊作為過關(guān)目標(biāo)消去的“磚塊”的途中不會落到螢?zāi)坏紫?。感興趣的小伙伴可以了解一下2022-03-03
Python產(chǎn)生batch數(shù)據(jù)的操作
這篇文章主要介紹了Python產(chǎn)生batch數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
python爬蟲系列Selenium定向爬取虎撲籃球圖片詳解
這篇文章主要介紹了python爬蟲系列Selenium定向爬取虎撲籃球圖片詳解,具有一定參考價值,喜歡的朋友可以了解下。2017-11-11

