PySimpleGUI/PySimpleGUIWeb/Demo Programs/Web_Timer.py

63 lines
2.2 KiB
Python
Raw Permalink Normal View History

2019-01-23 21:59:54 +00:00
#!/usr/bin/env python
import PySimpleGUIWeb as sg
import time
# ---------------- Create Form ----------------
layout = [
[sg.Text('', background_color='black')],
[sg.Text('00:00', size=(30, 1), font=('Helvetica', 30), justification='center',
text_color='white', key='text', background_color='black')],
2019-01-23 21:59:54 +00:00
[sg.Text('', background_color='black')],
[sg.Button('Pause', key='button', button_color=('white', '#001480')),
sg.Button('Reset', button_color=('white', '#007339'), key='Reset'),
sg.Exit(button_color=('white', '#8B1A1A'), key='Exit', )],
]
2019-01-23 21:59:54 +00:00
window = sg.Window('Running Timer', layout,
background_color='black', font='Helvetica 18')
2019-01-23 21:59:54 +00:00
# ---------------- main loop ----------------
current_time = 0
paused = False
start_time = int(round(time.time() * 100))
while True:
# --------- read and update window --------
2019-01-23 21:59:54 +00:00
if not paused:
event, values = window.read(timeout=0)
2019-01-23 21:59:54 +00:00
current_time = int(round(time.time() * 100)) - start_time
else:
event, values = window.read()
print(event, values) if event != sg.TIMEOUT_KEY else None
2019-01-23 21:59:54 +00:00
if event == 'button':
event = window[event].GetText()
2019-01-23 21:59:54 +00:00
# --------- Do Button Operations --------
2019-06-25 20:46:32 +00:00
if event in (None, 'Exit'): # ALWAYS give a way out of program
2019-01-23 21:59:54 +00:00
break
2019-06-25 20:46:32 +00:00
if event == 'Reset':
2019-01-23 21:59:54 +00:00
start_time = int(round(time.time() * 100))
current_time = 0
paused_time = start_time
2019-01-23 21:59:54 +00:00
elif event == 'Pause':
paused = True
paused_time = int(round(time.time() * 100))
element = window['button']
element.update(text='Run')
2019-01-23 21:59:54 +00:00
elif event == 'Run':
paused = False
start_time = start_time + int(round(time.time() * 100)) - paused_time
element = window['button']
element.update(text='Pause')
2019-01-23 21:59:54 +00:00
# --------- Display timer in window --------
window['text'].update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
(current_time //
100) % 60,
current_time % 100))
2019-01-23 21:59:54 +00:00
# --------- After loop --------
window.close()