用python寫一個windows消息提醒小程序
一、需求
上班時(shí),由于自己經(jīng)常coding到忘記時(shí)間,經(jīng)常會一坐坐很久,搞的勞資腰都不好了,所以沒事閑的寫了個久坐提醒的小程序。
二、功能描述
能設(shè)置時(shí)間間隔,過了間隔時(shí)間windows發(fā)出提醒消息,能定制消息內(nèi)容。
三、實(shí)現(xiàn)
用到的大概有,
python,
ttkbootstrap(bootstrap ui頁面),
plyer(windows消息),
threading(多線程包)
實(shí)現(xiàn)代碼(全):
import os
import time
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledText
from plyer import notification
import threading
# 線程開關(guān)
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):
# 未到時(shí)間 就睡覺
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,則變?yōu)榫又辛? 如果加side=LEFT 則縱向居中
txt1 = ttk.Entry(btn_frame, font=("微軟雅黑", 10), ) # width=200,
txt1.pack(side=LEFT, )
# 開啟發(fā)送消息線程
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()
# 等待線程結(jié)束
# 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, )
# 提醒內(nèi)容
a_frame = ttk.Frame(root, padding=(10, 0, 0, 0), border=10) # padding
a_frame.pack(side=TOP, fill=BOTH) # 如果去掉fill,則變?yōu)榫又辛? 如果加side=LEFT 則縱向居中
b_frame = ttk.Frame(root, padding=(10, 0, 0, 0), border=10)
b_frame.pack(side=TOP, fill=BOTH) # 如果去掉fill,則變?yōu)榫又辛? 如果加side=LEFT 則縱向居中
t_label = ttk.Label(
master=a_frame, text="提醒內(nèi)容:", 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)
# 初始化提醒內(nèi)容
text_area.delete('0.0', END) # 刪除內(nèi)容
# TODO 讀取文件內(nèi)容
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", # 設(shè)置主題
position=(960, 400), # 窗口所在的位置
minsize=(0, 0), # 窗口的最小寬高
maxsize=(1920, 1080), # 窗口的最大寬高
# resizable=None, # 設(shè)置窗口是否可以更改大小
# alpha=1.0, # 設(shè)置窗口的透明度(0.0完全透明)
)
bagel = setup_frame(app)
bagel.pack(side=LEFT, fill=BOTH, expand=YES)
app.mainloop()
四、打包
打包使用的是pyinstaller,簡單說明下,-w 不顯示命令行打包 -F 表示生成exe文件,然后后面的–hidden-import是因?yàn)?,不加plyer包打不進(jìn)去,不知道為啥,所以用打包參數(shù)的方式import了。
pyinstaller -w -F msg.py --hidden-import plyer.platforms.win.notification
五、效果


保護(hù)老腰完成~!
以上就是用python寫一個windows消息提醒小程序的詳細(xì)內(nèi)容,更多關(guān)于python消息提醒程序的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)以主程序的形式執(zhí)行模塊
這篇文章主要介紹了Python實(shí)現(xiàn)以主程序的形式執(zhí)行模塊,首先創(chuàng)建一個以christmastree的命名的模塊并定義一個全局變量創(chuàng)建一個名稱為fun_christmastree()的函數(shù)展開詳情,感興趣的朋友可以參考一下2022-06-06
python保留小數(shù)函數(shù)的幾種使用總結(jié)
本文主要介紹了python保留小數(shù)函數(shù)的幾種使用總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
linux系統(tǒng)使用python監(jiān)控apache服務(wù)器進(jìn)程腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python監(jiān)控apache服務(wù)器進(jìn)程的腳本,大家參考使用吧2014-01-01
用Python編寫一個鼠標(biāo)自動點(diǎn)擊程序詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于如何使用Python編寫一個鼠標(biāo)自動點(diǎn)擊程序,通過使用pyautogui庫,可以實(shí)現(xiàn)模擬鼠標(biāo)點(diǎn)擊的功能,文章還介紹了如何使用time庫和threading庫來控制點(diǎn)擊間隔時(shí)間和實(shí)現(xiàn)多線程,需要的朋友可以參考下2024-11-11
利用python實(shí)現(xiàn)你說我猜游戲的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于如何利用python實(shí)現(xiàn)你說我猜游戲的相關(guān)資料,用到的都是一些簡單的基礎(chǔ)的python語句,適合剛?cè)腴T的小白,可以嘗試跟著一起敲一下,感受一下編程中的樂趣,需要的朋友可以參考下2022-05-05
PyTorch中torch.matmul()函數(shù)常見用法總結(jié)
torch.matmul()也是一種類似于矩陣相乘操作的tensor連乘操作。但是它可以利用python中的廣播機(jī)制,處理一些維度不同的tensor結(jié)構(gòu)進(jìn)行相乘操作,這篇文章主要介紹了PyTorch中torch.matmul()函數(shù)用法總結(jié),需要的朋友可以參考下2023-04-04

