用python寫一個windows消息提醒小程序
一、需求
上班時,由于自己經常coding到忘記時間,經常會一坐坐很久,搞的勞資腰都不好了,所以沒事閑的寫了個久坐提醒的小程序。
二、功能描述
能設置時間間隔,過了間隔時間windows發(fā)出提醒消息,能定制消息內容。
三、實現(xiàn)
用到的大概有,
python,
ttkbootstrap(bootstrap ui頁面),
plyer(windows消息),
threading(多線程包)
實現(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 # 線程開關 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,則變?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() # 等待線程結束 # 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,則變?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="提醒內容:", 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包打不進去,不知道為啥,所以用打包參數(shù)的方式import了。
pyinstaller -w -F msg.py --hidden-import plyer.platforms.win.notification
五、效果
保護老腰完成~!
以上就是用python寫一個windows消息提醒小程序的詳細內容,更多關于python消息提醒程序的資料請關注腳本之家其它相關文章!
相關文章
Python實現(xiàn)以主程序的形式執(zhí)行模塊
這篇文章主要介紹了Python實現(xiàn)以主程序的形式執(zhí)行模塊,首先創(chuàng)建一個以christmastree的命名的模塊并定義一個全局變量創(chuàng)建一個名稱為fun_christmastree()的函數(shù)展開詳情,感興趣的朋友可以參考一下2022-06-06linux系統(tǒng)使用python監(jiān)控apache服務器進程腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python監(jiān)控apache服務器進程的腳本,大家參考使用吧2014-01-01PyTorch中torch.matmul()函數(shù)常見用法總結
torch.matmul()也是一種類似于矩陣相乘操作的tensor連乘操作。但是它可以利用python中的廣播機制,處理一些維度不同的tensor結構進行相乘操作,這篇文章主要介紹了PyTorch中torch.matmul()函數(shù)用法總結,需要的朋友可以參考下2023-04-04