2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
2021-04-12 19:50:52 +00:00
|
|
|
|
2019-10-03 01:13:58 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-09 16:28:07 +00:00
|
|
|
import time
|
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
"""
|
2019-10-03 01:13:58 +00:00
|
|
|
Timer Desktop Widget Creates a floating timer that is always on top of other windows You move it by grabbing anywhere on the window Good example of how to do a non-blocking, polling program using PySimpleGUI
|
|
|
|
Something like this can be used to poll hardware when running on a Pi
|
2019-06-26 15:09:42 +00:00
|
|
|
|
2018-10-18 18:56:08 +00:00
|
|
|
While the timer ticks are being generated by PySimpleGUI's "timeout" mechanism, the actual value
|
|
|
|
of the timer that is displayed comes from the system timer, time.time(). This guarantees an
|
|
|
|
accurate time value is displayed regardless of the accuracy of the PySimpleGUI timer tick. If
|
|
|
|
this design were not used, then the time value displayed would slowly drift by the amount of time
|
2019-06-26 15:09:42 +00:00
|
|
|
it takes to execute the PySimpleGUI read and update calls (not good!)
|
|
|
|
|
2021-04-12 19:50:52 +00:00
|
|
|
Copyright 2021 PySimpleGUI
|
2018-09-20 14:41:47 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2019-10-03 01:13:58 +00:00
|
|
|
def time_as_int():
|
|
|
|
return int(round(time.time() * 100))
|
2018-09-20 14:41:47 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
# ---------------- Create Form ----------------
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('Black')
|
2018-09-14 00:08:35 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
layout = [[sg.Text('')],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Text('', size=(8, 2), font=('Helvetica', 20),
|
|
|
|
justification='center', key='text')],
|
|
|
|
[sg.Button('Pause', key='-RUN-PAUSE-', button_color=('white', '#001480')),
|
|
|
|
sg.Button('Reset', button_color=('white', '#007339'), key='-RESET-'),
|
|
|
|
sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]
|
2018-09-14 00:08:35 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Running Timer', layout,
|
|
|
|
no_titlebar=True,
|
|
|
|
auto_size_buttons=False,
|
|
|
|
keep_on_top=True,
|
|
|
|
grab_anywhere=True,
|
2021-05-02 14:46:31 +00:00
|
|
|
element_padding=(0, 0),
|
|
|
|
finalize=True,
|
|
|
|
element_justification='c',
|
2021-04-12 19:50:52 +00:00
|
|
|
right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT)
|
2018-09-14 00:08:35 +00:00
|
|
|
|
2019-10-03 01:13:58 +00:00
|
|
|
current_time, paused_time, paused = 0, 0, False
|
|
|
|
start_time = time_as_int()
|
2019-10-23 20:10:03 +00:00
|
|
|
|
|
|
|
while True:
|
2018-09-11 12:38:38 +00:00
|
|
|
# --------- Read and update window --------
|
|
|
|
if not paused:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read(timeout=10)
|
2019-10-03 01:13:58 +00:00
|
|
|
current_time = time_as_int() - start_time
|
2018-09-11 12:38:38 +00:00
|
|
|
else:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2018-09-11 12:38:38 +00:00
|
|
|
# --------- Do Button Operations --------
|
2020-05-07 10:22:59 +00:00
|
|
|
if event in (sg.WIN_CLOSED, 'Exit'): # ALWAYS give a way out of program
|
2018-09-11 12:38:38 +00:00
|
|
|
break
|
2019-10-03 01:13:58 +00:00
|
|
|
if event == '-RESET-':
|
|
|
|
paused_time = start_time = time_as_int()
|
2018-09-11 12:38:38 +00:00
|
|
|
current_time = 0
|
2019-10-03 01:13:58 +00:00
|
|
|
elif event == '-RUN-PAUSE-':
|
|
|
|
paused = not paused
|
|
|
|
if paused:
|
|
|
|
paused_time = time_as_int()
|
|
|
|
else:
|
|
|
|
start_time = start_time + time_as_int() - paused_time
|
2019-10-23 20:10:03 +00:00
|
|
|
# Change button's text
|
|
|
|
window['-RUN-PAUSE-'].update('Run' if paused else 'Pause')
|
2021-04-12 19:50:52 +00:00
|
|
|
elif event == 'Edit Me':
|
|
|
|
sg.execute_editor(__file__)
|
2018-09-11 12:38:38 +00:00
|
|
|
# --------- Display timer in window --------
|
2019-10-23 20:10:03 +00:00
|
|
|
window['text'].update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
|
|
|
|
(current_time // 100) % 60,
|
|
|
|
current_time % 100))
|
|
|
|
window.close()
|