2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2018-09-28 18:57:37 +00:00
|
|
|
if sys.version_info[0] >= 3:
|
2018-09-27 20:24:09 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-28 18:57:37 +00:00
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
2018-07-25 10:40:14 +00:00
|
|
|
import time
|
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
# Window that doen't block
|
2018-07-29 12:57:28 +00:00
|
|
|
# good for applications with an loop that polls hardware
|
2018-07-25 10:40:14 +00:00
|
|
|
def StatusOutputExample():
|
|
|
|
# Create a text element that will be updated with status information on the GUI itself
|
|
|
|
# Create the rows
|
2018-09-24 22:01:00 +00:00
|
|
|
layout = [[sg.Text('Non-blocking GUI with updates')],
|
2018-09-06 20:20:37 +00:00
|
|
|
[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='output')],
|
2018-09-24 22:01:00 +00:00
|
|
|
[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)
|
2018-07-25 10:40:14 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Some place later in your code...
|
2018-09-24 22:01:00 +00:00
|
|
|
# You need to perform a ReadNonBlocking on your window every now and then or
|
2018-07-29 12:57:28 +00:00
|
|
|
# else it won't refresh.
|
2018-07-25 10:40:14 +00:00
|
|
|
#
|
2018-07-29 12:57:28 +00:00
|
|
|
# your program's main loop
|
|
|
|
i=0
|
|
|
|
while (True):
|
|
|
|
# This is the code that reads and updates your window
|
2018-10-15 20:07:23 +00:00
|
|
|
event, values = window.ReadNonBlocking()
|
2018-09-24 22:01:00 +00:00
|
|
|
window.FindElement('output').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'Quit' or values is None:
|
2018-07-25 10:40:14 +00:00
|
|
|
break
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'LED On':
|
2018-07-29 13:11:51 +00:00
|
|
|
print('Turning on the LED')
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'LED Off':
|
2018-07-29 13:11:51 +00:00
|
|
|
print('Turning off the LED')
|
|
|
|
|
2018-07-29 12:57:28 +00:00
|
|
|
i += 1
|
|
|
|
# Your code begins here
|
2018-07-25 10:40:14 +00:00
|
|
|
time.sleep(.01)
|
|
|
|
|
2018-07-29 12:57:28 +00:00
|
|
|
# Broke out of main loop. Close the window.
|
2018-09-24 22:01:00 +00:00
|
|
|
window.CloseNonBlocking()
|
2018-07-29 12:57:28 +00:00
|
|
|
|
|
|
|
|
2018-07-31 04:01:07 +00:00
|
|
|
def RemoteControlExample():
|
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
layout = [[sg.Text('Robotics Remote Control')],
|
2018-07-31 04:01:07 +00:00
|
|
|
[sg.T(' '*10), sg.RealtimeButton('Forward')],
|
|
|
|
[ sg.RealtimeButton('Left'), sg.T(' '*15), sg.RealtimeButton('Right')],
|
|
|
|
[sg.T(' '*10), sg.RealtimeButton('Reverse')],
|
|
|
|
[sg.T('')],
|
2018-08-09 17:18:04 +00:00
|
|
|
[sg.Quit(button_color=('black', 'orange'))]
|
2018-07-31 04:01:07 +00:00
|
|
|
]
|
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
window = sg.Window('Robotics Remote Control', auto_size_text=True).Layout(layout).Finalize()
|
2018-07-31 04:01:07 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Some place later in your code...
|
2018-09-24 22:01:00 +00:00
|
|
|
# You need to perform a ReadNonBlocking on your window every now and then or
|
2018-07-31 04:01:07 +00:00
|
|
|
# else it won't refresh.
|
|
|
|
#
|
|
|
|
# your program's main loop
|
|
|
|
while (True):
|
|
|
|
# This is the code that reads and updates your window
|
2018-09-24 22:01:00 +00:00
|
|
|
button, values = window.ReadNonBlocking()
|
2018-07-31 04:01:07 +00:00
|
|
|
if button is not None:
|
2018-09-04 23:43:22 +00:00
|
|
|
print(button)
|
2018-07-31 04:01:07 +00:00
|
|
|
if button == 'Quit' or values is None:
|
|
|
|
break
|
2018-08-09 17:18:04 +00:00
|
|
|
# time.sleep(.01)
|
2018-07-31 04:01:07 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
window.CloseNonBlocking()
|
2018-07-29 12:57:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-07-31 04:01:07 +00:00
|
|
|
RemoteControlExample()
|
2018-07-29 12:57:28 +00:00
|
|
|
StatusOutputExample()
|
2018-09-20 14:41:47 +00:00
|
|
|
sg.Popup('End of non-blocking demonstration')
|
2018-07-25 10:40:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
main()
|