Major demo refresh.. switched everything to new function names, new design patterns
Out with the old, in with the new!!
This commit is contained in:
parent
2a06683383
commit
a1f4c60271
68 changed files with 706 additions and 1863 deletions
|
@ -1,31 +1,28 @@
|
|||
import PySimpleGUI as sg
|
||||
import time
|
||||
|
||||
|
||||
# form that doen't block
|
||||
# Window that doen't block
|
||||
# good for applications with an loop that polls hardware
|
||||
def StatusOutputExample():
|
||||
# Make a form, but don't use context manager
|
||||
form = sg.FlexForm('Running Timer', auto_size_text=True)
|
||||
# Create a text element that will be updated with status information on the GUI itself
|
||||
# Create the rows
|
||||
form_rows = [[sg.Text('Non-blocking GUI with updates')],
|
||||
layout = [[sg.Text('Non-blocking GUI with updates')],
|
||||
[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='output')],
|
||||
[sg.ReadFormButton('LED On'), sg.ReadFormButton('LED Off'), sg.ReadFormButton('Quit')]]
|
||||
# Layout the rows of the form and perform a read. Indicate the form is non-blocking!
|
||||
form.LayoutAndRead(form_rows, non_blocking=True)
|
||||
[sg.ReadButton('LED On'), sg.ReadButton('LED Off'), sg.ReadButton('Quit')]]
|
||||
# Layout the rows of the Window and perform a read. Indicate the Window is non-blocking!
|
||||
window = sg.Window('Running Timer', auto_size_text=True).Layout(layout)
|
||||
|
||||
#
|
||||
# Some place later in your code...
|
||||
# You need to perform a ReadNonBlocking on your form every now and then or
|
||||
# You need to perform a ReadNonBlocking on your window every now and then or
|
||||
# else it won't refresh.
|
||||
#
|
||||
# your program's main loop
|
||||
i=0
|
||||
while (True):
|
||||
# This is the code that reads and updates your window
|
||||
form.FindElement('output').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
|
||||
button, values = form.ReadNonBlocking()
|
||||
button, values = window.ReadNonBlocking()
|
||||
window.FindElement('output').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
|
||||
if button == 'Quit' or values is None:
|
||||
break
|
||||
if button == 'LED On':
|
||||
|
@ -38,14 +35,12 @@ def StatusOutputExample():
|
|||
time.sleep(.01)
|
||||
|
||||
# Broke out of main loop. Close the window.
|
||||
form.CloseNonBlockingForm()
|
||||
window.CloseNonBlocking()
|
||||
|
||||
|
||||
def RemoteControlExample():
|
||||
# Make a form, but don't use context manager
|
||||
form = sg.FlexForm('Robotics Remote Control', auto_size_text=True)
|
||||
|
||||
form_rows = [[sg.Text('Robotics Remote Control')],
|
||||
layout = [[sg.Text('Robotics Remote Control')],
|
||||
[sg.T(' '*10), sg.RealtimeButton('Forward')],
|
||||
[ sg.RealtimeButton('Left'), sg.T(' '*15), sg.RealtimeButton('Right')],
|
||||
[sg.T(' '*10), sg.RealtimeButton('Reverse')],
|
||||
|
@ -53,55 +48,30 @@ def RemoteControlExample():
|
|||
[sg.Quit(button_color=('black', 'orange'))]
|
||||
]
|
||||
|
||||
form.LayoutAndRead(form_rows, non_blocking=True)
|
||||
window = sg.Window('Robotics Remote Control', auto_size_text=True).Layout(layout).Finalize()
|
||||
|
||||
#
|
||||
# Some place later in your code...
|
||||
# You need to perform a ReadNonBlocking on your form every now and then or
|
||||
# You need to perform a ReadNonBlocking on your window every now and then or
|
||||
# else it won't refresh.
|
||||
#
|
||||
# your program's main loop
|
||||
while (True):
|
||||
# This is the code that reads and updates your window
|
||||
button, values = form.ReadNonBlocking()
|
||||
button, values = window.ReadNonBlocking()
|
||||
if button is not None:
|
||||
print(button)
|
||||
if button == 'Quit' or values is None:
|
||||
break
|
||||
# time.sleep(.01)
|
||||
|
||||
form.CloseNonBlockingForm()
|
||||
window.CloseNonBlocking()
|
||||
|
||||
|
||||
|
||||
|
||||
# This design pattern follows the uses a context manager to better control the resources
|
||||
# It may not be realistic to use a context manager within an embedded (Pi) environment
|
||||
# If on a Pi, then consider the above design patterns instead
|
||||
def StatusOutputExample_context_manager():
|
||||
with sg.FlexForm('Running Timer', auto_size_text=True) as form:
|
||||
output_element = sg.Text('', size=(8, 2), font=('Helvetica', 20))
|
||||
form_rows = [[sg.Text('Non-blocking GUI with updates')],
|
||||
[output_element],
|
||||
[sg.SimpleButton('Quit')]]
|
||||
|
||||
form.LayoutAndRead(form_rows, non_blocking=True)
|
||||
|
||||
for i in range(1, 1000):
|
||||
output_element.Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
|
||||
button, values = form.ReadNonBlocking()
|
||||
if values is None or button == 'Quit':
|
||||
break
|
||||
time.sleep(.01)
|
||||
else:
|
||||
form.CloseNonBlockingForm()
|
||||
|
||||
def main():
|
||||
RemoteControlExample()
|
||||
StatusOutputExample()
|
||||
StatusOutputExample()
|
||||
sg.Popup('End of non-blocking demonstration')
|
||||
# StatusOutputExample_context_manager()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue