用Python做一个电脑通知小工具,超级方便

序言

Windows不是有个消息通知功能,挺喜欢这个功能的,但是不太方便使用,也懒得去研究,于是准备用Python自己写一个,通过设定通知的间隔时间来实现类似闹钟的效果,这样既不用听闹钟的吵闹声,又做到了定时通知的效果,比如定时通知埋头写代码的我们按时喝水。

说干就干,直接使用pyqt来设计成人人都可以用的工具。

效果展示


代码实战

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  # 定时器

主要代码

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('通知内容')

        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())

线程运行相关部分

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的小伙伴,我特地准备了大量的学习资料视频电子书等等,也在下方名片自取就好啦!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python微信通知是通过调用第三方接口实现的一种方式,可以实现向指定的微信用户发送消息的功能。通常使用的第三方接口有Server酱、PushBear等。 使用Python发送微信通知的步骤如下: 1. 注册一个微信公众号,获取appid和appsecret。 2. 在Python中使用requests库,向第三方接口发送请求。 3. 在请求中包含必要的参数,如接收消息的微信用户openid、消息标题和内容等。 4. 根据接口返回结果,判断发送是否成功。 下面以使用Server酱为例,来展示具体的实现步骤: 1. 首先,需要在https://sc.ftqq.com/3.version注册并获取SCKEY。 2. 在Python中引入requests库,并调用`requests.post()`方法发送POST请求。 3. 在请求中包含以下参数: - url: Server酱提供的接口地址,格式为`https://sc.ftqq.com/{SCKEY}.send`。 - data: 包含要发送的消息内容的字典,如`{'text': '消息标题', 'desp': '消息内容'}`。其中,'text'为消息标题,'desp'为消息内容。可以根据需要自行修改。 4. 根据请求返回的结果进行判断,如果返回的状态码为200,则表示发送成功。 下面是一个示例代码: ```python import requests def send_wechat_notification(title, content): url = 'https://sc.ftqq.com/{YOUR_SCKEY}.send' # 替换为自己的SCKEY data = { 'text': title, 'desp': content } response = requests.post(url, data=data) if response.status_code == 200: print('消息发送成功') else: print('消息发送失败') title = 'Python微信通知示例' content = '这是一条通过Python发送的微信通知。' send_wechat_notification(title, content) ``` 以上就是使用Python实现微信通知的简要步骤和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值