python写一个windows消息提醒小软件

一、需求

上班时,由于自己经常coding到忘记时间,经常会一坐坐很久,搞的劳资腰都不好了,所以没事闲的写了个久坐提醒的小程序。

二、功能描述

能设置时间间隔,过了间隔时间windows发出提醒消息,能定制消息内容。

三、实现

用到的大概有,
python,
ttkbootstrap(bootstrap ui页面),
plyer(windows消息),
threading(多线程包)

实现代码(全):

import os
import time

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledText
from plyer import notification
import threading

# 线程开关
global a_true
a_true = True
# 线程
thread1 = None
# 文本路径
path = 'd:/msg-notify.txt'


# ~休息提醒~


def notify(v, w_t):
    global a_true
    while a_true:
        notification.notify(
            title='~温馨提示~',
            message=v,
            app_icon=None,
            timeout=100,
        )
        wait_time = int(w_t) * 60
        for i in range(wait_time):
            # 未到时间 就睡觉
            if i != wait_time:
                if not a_true:
                    print('~~判断~~', a_true)
                    break
                time.sleep(1)

        # time.sleep(int(w_t))# * 60
    pass


def setup_frame(master):
    root = ttk.Frame(master, )

    btn_frame = ttk.Frame(root, padding=(10, 10, 10, 10), border=10)
    btn_frame.pack(side=TOP, fill=BOTH)  # 如果去掉fill,则变为居中了; 如果加side=LEFT 则纵向居中

    txt1 = ttk.Entry(btn_frame, font=("微软雅黑", 10), )  # width=200,
    txt1.pack(side=LEFT, )

    # 开启发送消息线程
    def send_msg():
        global thread1
        global a_true
        if thread1 is not None:
            a_true = False
            thread1.join(1)
            thread1.is_alive()
            print('~~~停止线程1~~~', thread1.is_alive())
        print('open_file_test')
        wait_time = txt1.get()
        value = text_area.get(1.0, 'end')
        thread1 = threading.Thread(target=notify, args=(value, wait_time,))
        a_true = True
        thread1.start()
        # 等待线程结束
        # thread1.join()
        print('~~~开启线程~~~')

    # 停止提醒线程
    def stop_msg():
        global thread1
        if thread1 is not None:
            global a_true
            a_true = False
            thread1.join(1)
            print('~~~停止线程2~~~')

    btn_1 = ttk.Button(btn_frame, text="开始提醒", command=send_msg, bootstyle=PRIMARY)
    btn_1.pack(side=LEFT, )
    btn_2 = ttk.Button(btn_frame, text="停止提醒", command=stop_msg, bootstyle=DANGER)
    btn_2.pack(side=LEFT, )

    # 提醒内容
    a_frame = ttk.Frame(root, padding=(10, 0, 0, 0), border=10)  # padding
    a_frame.pack(side=TOP, fill=BOTH)  # 如果去掉fill,则变为居中了; 如果加side=LEFT 则纵向居中

    b_frame = ttk.Frame(root, padding=(10, 0, 0, 0), border=10)
    b_frame.pack(side=TOP, fill=BOTH)  # 如果去掉fill,则变为居中了; 如果加side=LEFT 则纵向居中
    t_label = ttk.Label(
        master=a_frame, text="提醒内容:", font="-size 13 -weight bold",  # width=20,    # background='red' -weight bold
    )
    t_label.pack(side=LEFT)

    text_area = ScrolledText(master=b_frame, height=100, autohide=True)
    text_area.pack(side=TOP, fill=BOTH)

    # 初始化提醒内容
    text_area.delete('0.0', END)  # 删除内容

    # TODO 读取文件内容
    file_exist = os.path.exists(path)
    notify_str = None
    if file_exist:
        rio = open(path, )
        notify_str = rio.read()
        rio.close()
    print(notify_str)
    if notify_str is None or notify_str == '':
        text_area.insert('insert', '该活动活动了!\n动动手,动动脚~\n起飞~~~')
    return root


if __name__ == "__main__":
    app = ttk.Window(
        title='作息提醒',
        size=(380, 480),  # 窗口的大小 (宽, 高)
        themename="litera",  # 设置主题
        position=(960, 400),  # 窗口所在的位置
        minsize=(0, 0),  # 窗口的最小宽高
        maxsize=(1920, 1080),  # 窗口的最大宽高
        # resizable=None,  # 设置窗口是否可以更改大小
        # alpha=1.0,  # 设置窗口的透明度(0.0完全透明)
    )

    bagel = setup_frame(app)
    bagel.pack(side=LEFT, fill=BOTH, expand=YES)

    app.mainloop()

四、打包

打包使用的是pyinstaller,简单说明下,-w 不显示命令行打包 -F 表示生成exe文件,然后后面的–hidden-import是因为,不加plyer包打不进去,不知道为啥,所以用打包参数的方式import了。

pyinstaller -w -F msg.py --hidden-import plyer.platforms.win.notification

五、效果

在这里插入图片描述

在这里插入图片描述

保护老腰完成~!

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值