File: breakrem.pyw
   1 #!/usr/bin/python3
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # breakrem.pyw [minutes...]
  27 #
  28 # Periodically show a reminder suggesting to take a break. You can change
  29 # the default period via a single cmd-line argument, which is in minutes.
  30 #
  31 # Countdowns start when the current popup's `yes` button is clicked. The
  32 # script quits when a `no` from any of the popups is clicked.
  33 #
  34 # When not given a number, the default period between reminders is 15
  35 # minutes.
  36 
  37 
  38 from sys import argv
  39 from time import sleep
  40 from tkinter import Tk, messagebox
  41 
  42 
  43 # the default period in minutes
  44 minutes = 15
  45 
  46 # handle optional cmd-line arg to change default period (in minutes)
  47 if len(argv) > 1:
  48     try:
  49         n = int(argv[1])
  50         if n > 0:
  51             minutes = n
  52     except:
  53         pass
  54 
  55 startmsg = f'''
  56 This app keeps reminding you to take breaks from your
  57 computer with pop-ups every {minutes} minutes: time isn't
  58 counted while pop-ups are showing.
  59 
  60 The pop-ups will keep coming regularly as long as you
  61 keep pressing OK to dismiss them when they show up;
  62 pressing Cancel or the ✕ on the top corner will quit
  63 this app and stop the reminders.
  64 
  65 Note: you can start this app from the command-line with
  66 a positive whole number to set how many minutes to wait
  67 for each break reminder.
  68 '''.strip()
  69 
  70 breakmsg = f'''
  71 The {minutes} minutes have passed; time for a break
  72 
  73 To start the countdown for the next reminder, press OK
  74 For no more reminders, press Cancel or the ✕ at the top
  75 '''.strip()
  76 
  77 # library `tkinter` insists on showing a window, so get the top-level
  78 # window just to hide it, and prevent anything but pop-ups from appearing
  79 win = Tk()
  80 win.attributes('-topmost', True)
  81 win.withdraw()
  82 
  83 # start cycle of 1 or more time-breaks, only showing one pop-up at a time
  84 answer = messagebox.askquestion('Break Reminder', startmsg)
  85 
  86 # keep going until asked to quit
  87 while answer == 'yes':
  88     sleep(60 * minutes)
  89     answer = messagebox.askquestion('Time for a Break!', breakmsg)