Merge pull request #176 from MikeTheWatchGuy/Dev-latest

Compacted and commented code.  Make more efficient by reading non-blo…
This commit is contained in:
MikeTheWatchGuy 2018-09-09 13:26:13 -04:00 committed by GitHub
commit 73ed99a56c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 20 deletions

View File

@ -2,59 +2,68 @@ import PySimpleGUI as sg
import time import time
""" """
Timer Desktop Widget Timer Desktop Widget
Creates a floating timer that is always on top of other windows Creates a floating timer that is always on top of other windows
You move it by grabbing anywhere on the window You move it by grabbing anywhere on the window
Good example of how to do a non-blocking, polling program using PySimpleGUI 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"
""" """
# form that doen't block
# good for applications with an loop that polls hardware
def Timer(): def Timer():
# ---------------- Create Form ----------------
sg.ChangeLookAndFeel('Dark') sg.ChangeLookAndFeel('Dark')
sg.SetOptions(element_padding=(0,0)) sg.SetOptions(element_padding=(0,0))
# Make a form, but don't use context manager # Make a form, but don't use context manager
form = sg.FlexForm('Running Timer', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True) # Create the form layout
# Create a text element that will be updated with status information on the GUI itself
# Create the rows
form_rows = [[sg.Text('')], form_rows = [[sg.Text('')],
[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')], [sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
[sg.ReadFormButton('Pause', key='button'), sg.ReadFormButton('Reset'), sg.Exit(button_color=('white','firebrick4'))]] [sg.ReadFormButton('Pause', key='button'), sg.ReadFormButton('Reset'), sg.Exit(button_color=('white','firebrick4'))]]
# Layout the rows of the form and perform a read. Indicate the form is non-blocking! # Layout the rows of the form and perform a read. Indicate the form is non-blocking!
form = sg.FlexForm('Running Timer', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True)
form.Layout(form_rows) form.Layout(form_rows)
# #
# your program's main loop # ---------------- main loop ----------------
i = 0 current_time = 0
paused = False paused = False
start_time = int(round(time.time()*100))
while (True): while (True):
# This is the code that reads and updates your window # --------- Read and update window --------
if not paused:
button, values = form.ReadNonBlocking() button, values = form.ReadNonBlocking()
#form.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100)) current_time = int(round(time.time()*100)) - start_time
x = divmod(i, 100) else:
y = divmod(x[0], 60) button, values = form.Read()
form.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format(y[0], y[1], x[1]))
# --------- Do Button Operations --------
if values is None or button == 'Exit': if values is None or button == 'Exit':
break break
#print(button)
if button is 'Reset': if button is 'Reset':
i=0 start_time = int(round(time.time()*100))
current_time = 0
paused_time = start_time
elif button == 'Pause': elif button == 'Pause':
paused = True paused = True
paused_time = int(round(time.time()*100))
element = form.FindElement('button') element = form.FindElement('button')
element.Update(new_text='Run') element.Update(new_text='Run')
elif button == 'Run': elif button == 'Run':
paused = False paused = False
start_time = start_time + int(round(time.time()*100)) - paused_time
element = form.FindElement('button') element = form.FindElement('button')
element.Update(new_text='Pause') element.Update(new_text='Pause')
if not paused: # --------- Display timer in window --------
i += 1 form.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
# Your code begins here (current_time // 100) % 60,
current_time % 100))
time.sleep(.01) time.sleep(.01)
# --------- After loop --------
# Broke out of main loop. Close the window. # Broke out of main loop. Close the window.
form.CloseNonBlockingForm() form.CloseNonBlockingForm()