Updated Timer Demos and added 4-line OpenCV demo

This commit is contained in:
PySimpleGUI 2019-10-02 21:13:58 -04:00
parent f50a9a0bf4
commit 91fe043c11
3 changed files with 36 additions and 43 deletions

View File

@ -1,13 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
import time import time
""" """
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 SimpleGUI Can be used to poll hardware when running on a Pi 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
While the timer ticks are being generated by PySimpleGUI's "timeout" mechanism, the actual value 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 of the timer that is displayed comes from the system timer, time.time(). This guarantees an
@ -15,57 +12,48 @@ import time
this design were not used, then the time value displayed would slowly drift by the amount of time this design were not used, then the time value displayed would slowly drift by the amount of time
it takes to execute the PySimpleGUI read and update calls (not good!) it takes to execute the PySimpleGUI read and update calls (not good!)
NOTE - you will get a warning message printed when you exit using exit button.
It will look something like: invalid command name \"1616802625480StopMove\"
""" """
def time_as_int():
return int(round(time.time() * 100))
# ---------------- Create Form ---------------- # ---------------- Create Form ----------------
sg.ChangeLookAndFeel('Black') sg.ChangeLookAndFeel('Black')
sg.SetOptions(element_padding=(0, 0))
layout = [[sg.Text('')], layout = [[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.Button('Pause', key='button', button_color=('white', '#001480')), [sg.Button('Pause', key='-RUN-PAUSE-', button_color=('white', '#001480')),
sg.Button('Reset', button_color=('white', '#007339'), key='Reset'), sg.Button('Reset', button_color=('white', '#007339'), key='-RESET-'),
sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]] sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]
window = sg.Window('Running Timer', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True).Layout(layout) window = sg.Window('Running Timer', layout, no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True, element_padding=(0,0))
# ---------------- main loop ---------------- # ---------------- main loop ----------------
current_time = 0 current_time, paused_time, paused = 0, 0, False
paused = False start_time = time_as_int()
start_time = int(round(time.time() * 100))
while (True): while (True):
# --------- Read and update window -------- # --------- Read and update window --------
if not paused: if not paused:
event, values = window.Read(timeout=10) event, values = window.Read(timeout=10)
current_time = int(round(time.time() * 100)) - start_time current_time = time_as_int() - start_time
else: else:
event, values = window.Read() event, values = window.Read()
if event == 'button':
event = window.FindElement(event).GetText()
# --------- Do Button Operations -------- # --------- Do Button Operations --------
if event in (None, 'Exit'): # ALWAYS give a way out of program if event in (None, 'Exit'): # ALWAYS give a way out of program
break break
if event == 'Reset': if event == '-RESET-':
start_time = int(round(time.time() * 100)) paused_time = start_time = time_as_int()
current_time = 0 current_time = 0
paused_time = start_time elif event == '-RUN-PAUSE-':
elif event == 'Pause': paused = not paused
paused = True if paused:
paused_time = int(round(time.time() * 100)) paused_time = time_as_int()
element = window.FindElement('button') else:
element.Update(text='Run') start_time = start_time + time_as_int() - paused_time
elif event == 'Run': window['-RUN-PAUSE-'].Update('Run' if paused else 'Pause') # Change button's text
paused = False
start_time = start_time + int(round(time.time() * 100)) - paused_time
element = window.FindElement('button')
element.Update(text='Pause')
# --------- Display timer in window -------- # --------- Display timer in window --------
window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60, window['text'].Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
(current_time // 100) % 60, (current_time // 100) % 60,
current_time % 100)) current_time % 100))
window.close()
# --------- After loop --------

View File

@ -0,0 +1,4 @@
import cv2, PySimpleGUI as sg
window, cap = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(filename='', key='image')],], location=(800,400)), cv2.VideoCapture(0)
while window(timeout=20)[0] is not None:
window['image'](data=cv2.imencode('.png', cap.read()[1])[1].tobytes())

View File

@ -7,37 +7,38 @@ def Timer():
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) window = sg.Window('Running Timer', no_titlebar=True, auto_size_buttons=False)
# Create a text element that will be updated with status information on the GUI itself # Create a text element that will be updated with status information on the GUI itself
# Create the rows # 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'), sg.ReadFormButton('Reset'), sg.Exit(button_color=('white','firebrick4'))]] [sg.Button('Pause', key='-RUN-PAUSE-'), sg.Button('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.Layout(form_rows) window.Layout(form_rows)
# #
# your program's main loop # your program's main loop
i = 0 i = 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 # This is the code that reads and updates your window
button, values = form.ReadNonBlocking() button, values = window.read(timeout=0)
form.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100)) window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
if values is None or button == 'Exit': if values is None or button == 'Exit':
break break
if button == 'Reset': if button == 'Reset':
i=0 i=0
elif button == 'Pause': elif button == '-RUN-PAUSE-':
paused = not paused paused = not paused
window['-RUN-PAUSE-'].Update('Run' if paused else 'Pause')
if not paused: if not paused:
i += 1 i += 1
# Your code begins here
time.sleep(.01)
# Broke out of main loop. Close the window. # Broke out of main loop. Close the window.
form.CloseNonBlockingForm() window.close()
Timer() Timer()