用python寫一個(gè)windows消息提醒小程序
一、需求
上班時(shí),由于自己經(jīng)常coding到忘記時(shí)間,經(jīng)常會(huì)一坐坐很久,搞的勞資腰都不好了,所以沒事閑的寫了個(gè)久坐提醒的小程序。
二、功能描述
能設(shè)置時(shí)間間隔,過(guò)了間隔時(shí)間windows發(fā)出提醒消息,能定制消息內(nèi)容。
三、實(shí)現(xiàn)
用到的大概有,
python,
ttkbootstrap(bootstrap ui頁(yè)面),
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', '該活動(dòng)活動(dòng)了!\n動(dòng)動(dòng)手,動(dòng)動(dòng)腳~\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,簡(jiǎn)單說(shuō)明下,-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寫一個(gè)windows消息提醒小程序的詳細(xì)內(nèi)容,更多關(guān)于python消息提醒程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python正則表達(dá)式re.sub各個(gè)參數(shù)的超詳細(xì)講解
Python 的 re 模塊提供了re.sub用于替換字符串中的匹配項(xiàng),下面這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式re.sub各個(gè)參數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
詳解分布式系統(tǒng)中如何用python實(shí)現(xiàn)Paxos
提到分布式算法,就不得不提 Paxos 算法,在過(guò)去幾十年里,它基本上是分布式共識(shí)的代 名詞,因?yàn)楫?dāng)前最常用的一批共識(shí)算法都是基于它改進(jìn)的。比如,F(xiàn)ast Paxos 算法、 Cheap Paxos 算法、Raft 算法、ZAB 協(xié)議等等。2021-05-05
Python圖像處理庫(kù)PIL的ImageEnhance模塊使用介紹
這篇文章主要介紹了Python圖像處理庫(kù)PIL的ImageEnhance模塊使用介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python實(shí)現(xiàn)PDF轉(zhuǎn)Markdown的完整方案與代碼
PDF作為廣泛使用的文檔格式,轉(zhuǎn)換為輕量級(jí)標(biāo)記語(yǔ)言Markdown后,可無(wú)縫集成到技術(shù)文檔中,所以下面我們就來(lái)看看如何使用Python語(yǔ)言實(shí)現(xiàn)這一功能吧2025-07-07
一文搞懂???????python可迭代對(duì)象,迭代器,生成器,協(xié)程
這篇文章主要介紹了一文搞懂???????python可迭代對(duì)象,迭代器,生成器,協(xié)程,微博吱嘎部分圍繞主題展開詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05
Tensorflow加載與預(yù)處理數(shù)據(jù)詳解實(shí)現(xiàn)方法
讀取大型數(shù)據(jù)集并對(duì)其進(jìn)行有效預(yù)處理可能對(duì)其他深度學(xué)習(xí)庫(kù)來(lái)說(shuō)很難實(shí)現(xiàn),但是TensorFlow借助Data API很容易實(shí)現(xiàn):只需創(chuàng)建一個(gè)數(shù)據(jù)集對(duì)象,并告訴它如何從何處獲取數(shù)據(jù)以及如何對(duì)其進(jìn)行轉(zhuǎn)換2022-11-11
python機(jī)器學(xué)習(xí)之隨機(jī)森林(七)
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)之隨機(jī)森林,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
python3使用pandas獲取股票數(shù)據(jù)的方法
今天小編就為大家分享一篇python3使用pandas獲取股票數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python實(shí)現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中隊(duì)列的操作方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中隊(duì)列的操作方法,結(jié)合實(shí)例形式演示了Python針對(duì)數(shù)據(jù)結(jié)構(gòu)中隊(duì)列的初始化、插入、刪除、判斷隊(duì)列滿及隊(duì)列空等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

