2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2018-09-28 18:57:37 +00:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3:
|
2018-09-27 20:24:09 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-28 18:57:37 +00:00
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
2018-09-09 16:28:07 +00:00
|
|
|
import time
|
|
|
|
|
2018-09-20 14:41:47 +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 Can be used to poll hardware when running on a Pi NOTE - you will get a warning message printed when you exit using exit button.
|
|
|
|
It will look something like: invalid command name \"1616802625480StopMove\"
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
# ---------------- Create Form ----------------
|
|
|
|
sg.ChangeLookAndFeel('Black')
|
|
|
|
sg.SetOptions(element_padding=(0, 0))
|
2018-09-14 00:08:35 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
layout = [[sg.Text('')],
|
|
|
|
[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
|
|
|
|
[sg.ReadButton('Pause', key='button', button_color=('white', '#001480')),
|
|
|
|
sg.ReadButton('Reset', button_color=('white', '#007339'), key='Reset'),
|
|
|
|
sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]
|
2018-09-14 00:08:35 +00:00
|
|
|
|
2018-10-08 17:30:33 +00:00
|
|
|
window = sg.Window('Running Timer', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True).Layout(layout)
|
2018-09-14 00:08:35 +00:00
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
# ---------------- main loop ----------------
|
|
|
|
current_time = 0
|
|
|
|
paused = False
|
|
|
|
start_time = int(round(time.time() * 100))
|
|
|
|
while (True):
|
|
|
|
# --------- Read and update window --------
|
|
|
|
if not paused:
|
2018-09-24 22:01:00 +00:00
|
|
|
button, values = window.ReadNonBlocking()
|
2018-09-11 12:38:38 +00:00
|
|
|
current_time = int(round(time.time() * 100)) - start_time
|
|
|
|
else:
|
2018-09-24 22:01:00 +00:00
|
|
|
button, values = window.Read()
|
2018-09-20 14:41:47 +00:00
|
|
|
if button == 'button':
|
2018-09-24 22:01:00 +00:00
|
|
|
button = window.FindElement(button).GetText()
|
2018-09-11 12:38:38 +00:00
|
|
|
# --------- Do Button Operations --------
|
|
|
|
if values is None or button == 'Exit':
|
|
|
|
break
|
|
|
|
if button is 'Reset':
|
|
|
|
start_time = int(round(time.time() * 100))
|
|
|
|
current_time = 0
|
|
|
|
paused_time = start_time
|
|
|
|
elif button == 'Pause':
|
|
|
|
paused = True
|
|
|
|
paused_time = int(round(time.time() * 100))
|
2018-09-24 22:01:00 +00:00
|
|
|
element = window.FindElement('button')
|
2018-09-11 12:38:38 +00:00
|
|
|
element.Update(text='Run')
|
|
|
|
elif button == 'Run':
|
|
|
|
paused = False
|
|
|
|
start_time = start_time + int(round(time.time() * 100)) - paused_time
|
2018-09-24 22:01:00 +00:00
|
|
|
element = window.FindElement('button')
|
2018-09-11 12:38:38 +00:00
|
|
|
element.Update(text='Pause')
|
2018-09-09 16:28:07 +00:00
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
# --------- Display timer in window --------
|
2018-09-24 22:01:00 +00:00
|
|
|
window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
|
2018-09-11 12:38:38 +00:00
|
|
|
(current_time // 100) % 60,
|
|
|
|
current_time % 100))
|
|
|
|
time.sleep(.01)
|
2018-09-09 17:25:33 +00:00
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
# --------- After loop --------
|
2018-09-09 16:28:07 +00:00
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
# Broke out of main loop. Close the window.
|
2018-09-24 22:01:00 +00:00
|
|
|
window.CloseNonBlocking()
|