PySimpleGUI/Demo_Timer.py

43 lines
1.5 KiB
Python
Raw Normal View History

2018-09-04 23:47:14 +00:00
import PySimpleGUI as sg
import time
# form that doen't block
# good for applications with an loop that polls hardware
def Timer():
sg.ChangeLookAndFeel('Dark')
2018-09-07 22:04:23 +00:00
sg.SetOptions(element_padding=(0,0))
2018-09-04 23:47:14 +00:00
# Make a form, but don't use context manager
2018-09-08 20:26:38 +00:00
form = sg.FlexForm('Running Timer', no_titlebar=True, auto_size_buttons=False)
2018-09-04 23:47:14 +00:00
# Create a text element that will be updated with status information on the GUI itself
# Create the rows
2018-09-07 22:04:23 +00:00
form_rows = [[sg.Text('')],
2018-09-04 23:47:14 +00:00
[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
2018-09-07 22:04:23 +00:00
[sg.ReadFormButton('Pause'), sg.ReadFormButton('Reset'), sg.Exit(button_color=('white','firebrick4'))]]
2018-09-04 23:47:14 +00:00
# Layout the rows of the form and perform a read. Indicate the form is non-blocking!
form.Layout(form_rows)
2018-09-04 23:47:14 +00:00
#
# your program's main loop
i = 0
paused = False
while (True):
# This is the code that reads and updates your window
button, values = form.ReadNonBlocking()
form.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
2018-09-04 23:47:14 +00:00
if values is None or button == 'Exit':
2018-09-04 23:47:14 +00:00
break
if button is 'Reset':
i=0
elif button is 'Pause':
2018-09-04 23:47:14 +00:00
paused = not paused
if not paused:
i += 1
# Your code begins here
time.sleep(.01)
# Broke out of main loop. Close the window.
form.CloseNonBlockingForm()
Timer()