# ==============================================================================
"""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) # create frame for the 7 buttons
  Button(frame, text='|<', width=5, command=lambda: on_but(0))
  Button(frame, text='<<', width=5, command=lambda: on_but(1))
  Button(frame, text='<',  width=5, command=lambda: on_but(2))
  Button(frame, text='0',  width=5, command=lambda: on_but(3))
  Button(frame, text='>',  width=5, command=lambda: on_but(4))
  Button(frame, text='>>', width=5, command=lambda: on_but(5))
  Button(frame, text='>|', width=5, command=lambda: on_but(6))
  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"""
  value = win.counter['text']    # get current counter value
  if   index == 0: value = -1000 # action for button: |<
  elif index == 1: value -= 10   # action for button: <<
  elif index == 2: value -= 1    # action for button: <
  elif index == 3: value = 0     # action for button: 0
  elif index == 4: value += 1    # action for button: >
  elif index == 5: value += 10   # action for button: >>
  elif index == 6: value = 1000  # action for button: >|
  win.counter['text'] = min(1000, max(-1000, value)) # clamp counter value
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
