File: later.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 # later.pyw
  27 #
  28 # Show a future reminder: both the exact message/reminder and wait-time are
  29 # set in the GUI shown. Escape quits without starting any future reminder.
  30 
  31 
  32 from tkinter import Tk, Button, Entry, EventType, Frame, Label
  33 from tkinter.messagebox import showinfo
  34 
  35 
  36 def remind_and_quit(minutes: int, msg: str) -> None:
  37     if minutes < 60:
  38         showinfo(f'Reminder from {minutes} minutes ago', msg)
  39     else:
  40         showinfo(f'Reminder from {minutes/60} hours ago', msg)
  41     win.quit()
  42 
  43 
  44 def check_shortcuts(e: EventType) -> None:
  45     char = e.char
  46     if char == '':
  47         return
  48     if ord(char) == 27:
  49         win.quit()
  50 
  51 
  52 def make_button(to, caption: str, minutes: int) -> Button:
  53     def react() -> None:
  54         msg = input.get().strip()
  55         if msg == '':
  56             msg = '<no specific message>'
  57         win.withdraw()
  58         ms = 1_000 * 60 * minutes
  59         win.after(ms, lambda: remind_and_quit(minutes, msg))
  60 
  61     def check_enter(e: EventType) -> None:
  62         char = e.char
  63         if char == '':
  64             return
  65         if ord(char) == 13:
  66             react()
  67 
  68     b = Button(to, text=caption, command=react, width=8)
  69     b.bind('<KeyPress>', check_enter)
  70     return b
  71 
  72 
  73 win = Tk()
  74 win.title('Remind Me Later')
  75 win.bind('<KeyPress>', lambda e: check_shortcuts(e))
  76 input = Entry(width=54)
  77 input.pack()
  78 input.focus()
  79 fr = Frame(win)
  80 for pos, minutes in enumerate([5, 10, 15, 20, 30, 45]):
  81     make_button(fr, f'{minutes} min', minutes).grid(row=0, column=pos)
  82 Label(fr).grid(row=1, column=0)
  83 for pos, hours in enumerate(range(1, 6 + 1)):
  84     make_button(fr, f'{hours} hr', 60 * hours).grid(row=2, column=pos)
  85 for pos, hours in enumerate(range(7, 12 + 1)):
  86     make_button(fr, f'{hours} hr', 60 * hours).grid(row=3, column=pos)
  87 fr.pack()
  88 win.mainloop()