This article is participating in Python Theme Month. See the link to the event for more details

background

A colleague of mine told me today that he always forgets to drink water. What can I do about it? As programmers, we are sedentary and lack of exercise every day. If we forget to drink water, it will bring serious consequences. So I decided to make a small tool for him to remind him to drink water and do activities regularly.

preparation

Downloading dependent software

pip install plyer
Copy the code

code

from plyer import notification
import schedule,time

def notify() :
    notification.notify(title="Warm tips",
                        message="Time for water, please get up and stretch! - O (studying studying) O -",
                        timeout=10
                        )

schedule.every(10).seconds.do(notify)

while True:
    schedule.run_pending()
    time.sleep(1)
Copy the code

The effect

For the convenience of demonstration, we set the scheduled task to 10s every time, and set it to 1 hour in official use.

Package as an executable file

When a BUG occurs during packaging using the code above, the packages associated with notification and Schedule cannot be found. However, the packaging tool has already inserted the dependent libraries into the EXE by default. We don’t know why there is still a problem, so we temporarily changed the scheduled task and sent the popover package.

Software selection

  • Packaging tool selection:pyinstaller
  • Popover reminder selection:pywin32

Code – new

import win32api,win32con,time
from apscheduler.schedulers.blocking import BlockingScheduler

def DrunkWater() :
    win32api.MessageBox(0."It's time for water, get up and stretch!"."Warm tips",win32con.MB_OK)

# BlockingScheduler
scheduler = BlockingScheduler()
scheduler.add_job(DrunkWater, 'interval', minutes=1)


if __name__ == '__main__':

    while True:
        scheduler.start()
        time.sleep(1)
Copy the code

packaging

We plan to be reminded once an hour, so when packing, change the scheduled task time to 60 minutes.

Install PyInstaller & PyWin32

pip install pyinstaller
pip install pywin32
Copy the code

Packaging orders

Run pyinstaller-f -w demp.py in the path where the py file is stored.

-f: Packages the code into a separate executable file. -w: Indicates that the command runs in noconsole mode. That is, there is no CMD black box.

The results of

As mentioned above, the file in dist is our packaged executable file that performs the same functions as demo.py.

How’s it going? Are you a loser?

That’s all for today, thanks for reading, see you next time.