ClearCode

Stopwatch

Apr 1st, 2022
2,491
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 2 0
  1. import PySimpleGUI as sg
  2. from time import time
  3.  
  4. def create_window():
  5.     sg.theme('black')
  6.     layout = [
  7.         [sg.Push(), sg.Image('cross.png', pad = 0, enable_events = True, key = '-CLOSE-')],
  8.         [sg.VPush()],
  9.         [sg.Text('', font = 'Young 50', key = '-TIME-')],
  10.         [
  11.             sg.Button('Start', button_color = ('#FFFFFF','#FF0000'), border_width = 0, key = '-STARTSTOP-'),
  12.             sg.Button('Lap', button_color = ('#FFFFFF','#FF0000'), border_width = 0, key = '-LAP-', visible = False)
  13.         ],
  14.         [sg.Column([[]], key = '-LAPS-')],
  15.         [sg.VPush()]
  16.     ]
  17.  
  18.     return sg.Window(
  19.         'Stopwatch',
  20.         layout,
  21.         size = (300,300),
  22.         no_titlebar = True,
  23.         element_justification = 'center')
  24.  
  25. window = create_window()
  26. start_time = 0
  27. active = False
  28. lap_amount = 1
  29.  
  30. while True:
  31.     event, values = window.read(timeout = 10)
  32.     if event in (sg.WIN_CLOSED, '-CLOSE-'):
  33.         break
  34.  
  35.     if event == '-STARTSTOP-':
  36.         if active:
  37.             # from active to stop
  38.             active = False
  39.             window['-STARTSTOP-'].update('Reset')
  40.             window['-LAP-'].update(visible = False)
  41.         else:
  42.             # from stop to reset
  43.             if start_time > 0:
  44.                 window.close()
  45.                 window = create_window()
  46.                 start_time = 0
  47.                 lap_amount = 1
  48.             # from start to active
  49.             else:
  50.                 start_time = time()
  51.                 active = True
  52.                 window['-STARTSTOP-'].update('Stop')
  53.                 window['-LAP-'].update(visible = True)
  54.  
  55.     if active:
  56.         elapsed_time = round(time() - start_time,1)
  57.         window['-TIME-'].update(elapsed_time)
  58.  
  59.     if event == '-LAP-':
  60.         window.extend_layout(window['-LAPS-'], [[sg.Text(lap_amount),sg.VSeparator(),sg.Text(elapsed_time)]])
  61.         lap_amount += 1
  62.        
  63. window.close()
Advertisement
Add Comment
Please, Sign In to add comment