# ==============================================================================
"""CHRONO : a user-controlled digital stopwatch"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
# ------------------------------------------------------------------------------
def main():
  """create the main window and pack the widgets"""
  global win
  win = Win(title='CHRONO', op=5) # create main window
  font1, font2 = 'Arial 16', 'Times 120 bold' # define fonts for widgets
  # ----------------------------------------------------------------------------
  frame = Frame(win, grow=False, font=font1) # create frame for buttons
  Button(frame, text=('START','STOP'), width=9, command=on_start) # multi state
  Button(frame, text='RESET', width=9, command=on_reset) # single state
  # ----------------------------------------------------------------------------
  colors = ('#F00','#0F0','#00F','#0FF','#F0F','#FF0') # store colors for chrono
  Label(win, font=font2, width=5, fg=colors, border=2) # create chrono widget
  # ----------------------------------------------------------------------------
  win.start, win.chrono = frame[0], win[1] # friendly names for widgets 
  on_reset() # invoke callback for button 'RESET'
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def on_reset() -> None:
  """callback function for the 'RESET' button"""
  win.chrono['text'] = win.chrono.state = 0 # reset value and state for chrono 
# ------------------------------------------------------------------------------
def on_start() -> None:
  """callback function for the 'START/STOP' button"""
  win.start.state = 1-win.start.state # switch button state (state <--> 1-state)
  if win.start.state == 1: tick() # call 'tick' function when button state is 1
# ------------------------------------------------------------------------------
def tick() -> None:
  """increment counter and make recursive function call after 10ms"""
  win.chrono['text'] += 1 # increment counter value
  win.chrono.state = win.chrono['text']//50 # change chrono state every 50 steps
  if win.start.state == 1: win.after(10, tick) # call next tick after 10ms
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
