# ==============================================================================
"""COUNTER : a user-controlled digital counter"""
# ==============================================================================
__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; font1, font2 = 'Courier 16 bold', 'Arial 96 bold'
  win = Win(title='COUNTER', font=font1, op=5) # create main window
  # ----------------------------------------------------------------------------
  frame = Frame(win, font=font1) # create a frame for the 7 buttons widgets
  text = '|< << < 0 > >> >|'.split() # create a list for the 7 button strings
  for n in range(7): # loop to create the 7 control buttons
    Button(frame, text=text[n], width=5, command=lambda n=n: on_but(n))
  Label(win, text=0, font=font2, border=2) # create counter widget
  # ----------------------------------------------------------------------------
  win.counter = win[1] # friendly name for the counter widget
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def on_but(index:int) -> None:
  """generic callback function for all seven buttons"""
  slow, fast, minval, maxval = 1, 10, -1000, 1000 # set counter parameters
  value = win.counter['text'] # get current counter value
  # create a tuple containing all 7 new counter values, one for each button
  values = (minval, value-fast, value-slow, 0, value+slow, value+fast, maxval) 
  value = values[index] # select counter value using the provided button index
  win.counter['text'] = min(maxval, max(minval, value)) # clamp counter value
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
