From 91fe043c11adcb9c5c63469246d96ca9f9cfe3ec Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Wed, 2 Oct 2019 21:13:58 -0400 Subject: [PATCH] Updated Timer Demos and added 4-line OpenCV demo --- DemoPrograms/Demo_Desktop_Widget_Timer.py | 56 +++++++++------------- DemoPrograms/Demo_OpenCV_4_Line_Program.py | 4 ++ DemoPrograms/Demo_Timer.py | 19 ++++---- 3 files changed, 36 insertions(+), 43 deletions(-) create mode 100644 DemoPrograms/Demo_OpenCV_4_Line_Program.py diff --git a/DemoPrograms/Demo_Desktop_Widget_Timer.py b/DemoPrograms/Demo_Desktop_Widget_Timer.py index 5d80653f..31b24875 100644 --- a/DemoPrograms/Demo_Desktop_Widget_Timer.py +++ b/DemoPrograms/Demo_Desktop_Widget_Timer.py @@ -1,13 +1,10 @@ #!/usr/bin/env python -import sys -if sys.version_info[0] >= 3: - import PySimpleGUI as sg -else: - import PySimpleGUI27 as sg +import PySimpleGUI as sg 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 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 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 ---------------- sg.ChangeLookAndFeel('Black') -sg.SetOptions(element_padding=(0, 0)) layout = [[sg.Text('')], [sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')], - [sg.Button('Pause', key='button', button_color=('white', '#001480')), - sg.Button('Reset', button_color=('white', '#007339'), key='Reset'), + [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')]] -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 ---------------- -current_time = 0 -paused = False -start_time = int(round(time.time() * 100)) +current_time, paused_time, paused = 0, 0, False +start_time = time_as_int() while (True): # --------- Read and update window -------- if not paused: event, values = window.Read(timeout=10) - current_time = int(round(time.time() * 100)) - start_time + current_time = time_as_int() - start_time else: event, values = window.Read() - if event == 'button': - event = window.FindElement(event).GetText() # --------- Do Button Operations -------- if event in (None, 'Exit'): # ALWAYS give a way out of program break - if event == 'Reset': - start_time = int(round(time.time() * 100)) + if event == '-RESET-': + paused_time = start_time = time_as_int() current_time = 0 - paused_time = start_time - elif event == 'Pause': - paused = True - paused_time = int(round(time.time() * 100)) - element = window.FindElement('button') - element.Update(text='Run') - elif event == 'Run': - paused = False - start_time = start_time + int(round(time.time() * 100)) - paused_time - element = window.FindElement('button') - element.Update(text='Pause') + elif event == '-RUN-PAUSE-': + paused = not paused + if paused: + paused_time = time_as_int() + else: + start_time = start_time + time_as_int() - paused_time + window['-RUN-PAUSE-'].Update('Run' if paused else 'Pause') # Change button's text # --------- 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)) - -# --------- After loop -------- +window.close() \ No newline at end of file diff --git a/DemoPrograms/Demo_OpenCV_4_Line_Program.py b/DemoPrograms/Demo_OpenCV_4_Line_Program.py new file mode 100644 index 00000000..6e032cf3 --- /dev/null +++ b/DemoPrograms/Demo_OpenCV_4_Line_Program.py @@ -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()) diff --git a/DemoPrograms/Demo_Timer.py b/DemoPrograms/Demo_Timer.py index ec457091..4bf3d386 100644 --- a/DemoPrograms/Demo_Timer.py +++ b/DemoPrograms/Demo_Timer.py @@ -7,37 +7,38 @@ def Timer(): sg.ChangeLookAndFeel('Dark') sg.SetOptions(element_padding=(0,0)) # 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 the rows form_rows = [[sg.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! - form.Layout(form_rows) + window.Layout(form_rows) # # your program's main loop i = 0 paused = False + start_time = int(round(time.time() * 100)) 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)) + button, values = window.read(timeout=0) + window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100)) if values is None or button == 'Exit': break if button == 'Reset': i=0 - elif button == 'Pause': + elif button == '-RUN-PAUSE-': paused = not paused + window['-RUN-PAUSE-'].Update('Run' if paused else 'Pause') if not paused: i += 1 - # Your code begins here - time.sleep(.01) + # Broke out of main loop. Close the window. - form.CloseNonBlockingForm() + window.close() Timer() \ No newline at end of file