Part1背景
我有个同事今天跟我说, 他总是忘记喝水,这可怎么办?作为程序员的我们,本就每天久坐缺乏锻炼,如果再忘记喝水,那将会带来多么严重的后果,所以我决定为他做一个小工具来提醒他定时喝水&活动。
Part2前期准备
1下载依赖软件
pip install plyer
代码
from plyer import notification
import schedule,time
def notify():
notification.notify(title="温馨提示",
message="喝水时间到,请起来活动活动! --O(∩_∩)O--",
timeout=10
)
schedule.every(10).seconds.do(notify)
while True:
schedule.run_pending()
time.sleep(1)
效果
提醒效果
为了演示方便,我们将定时任务设置为了 10s
一次,正式使用的时候我们设置为 1
个小时即可。
Part3打包为可执行文件
使用如上代码在打包时当发生了 BUG
,notification
和 schedule
相关的包找不到。但是打包工具默认已经把依赖库打进了 exe
,不知道为何还有问题,所以我们临时更换了定时任务和发送弹窗的包。
2软件选择
打包工具选择:pyinstaller
弹窗提醒选择:pywin32
3代码-new
import win32api,win32con,time
from apscheduler.schedulers.blocking import BlockingScheduler
def DrunkWater():
win32api.MessageBox(0, "喝水时间到,起来活动活动吧!", "温馨提示",win32con.MB_OK)
# BlockingScheduler
scheduler = BlockingScheduler()
scheduler.add_job(DrunkWater, 'interval', minutes=1)
if __name__ == '__main__':
while True:
scheduler.start()
time.sleep(1)
效果图
4打包
我们计划一个小时提醒一次,所以在打包的时候将定时任务时间改为 60
分钟即可。
安装 pyinstaller & pywin32
pip install pyinstaller
pip install pywin32
打包命令
直接在 py
文件所在路径下执行:pyinstaller -F -w demp.py
即可。
-F
:意为将代码打包成一个独立的可执行文件。
-w
:意为以 noconsole
模式运行,即没有 cmd
黑框。
结果
打包结果
如上如,dist
中的文件就是我们打包后的可执行文件,其能够实现和 demo.py
一样的功能。
怎么样,你学废了吗?
以上就是今天的全部内容了,感谢您的阅读,我们下节再会。
完
往期推荐
经典实践 | 网速测速小工具(上)
经典实践 | 网速测速小工具(下)
Schedule | 轻量化的定时任务框架
Partial | 超有用的偏函数
PyWebIo | 快速构建web应用
5分钟带你了解Scrum
5分钟学会本地Pypi源搭建
FastApi-18-Token生成
FastApi-19-Token校验
FastApi-20-Token获取和使用
FastApi-21-APIRouter
点个
在看
你最好看