PySimpleGUI/DemoPrograms/Demo_Pi_Robotics.py

101 lines
3.8 KiB
Python
Raw Normal View History

2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
import sys
import PySimpleGUI as sg
2018-07-31 14:53:55 +00:00
# Robotics design pattern
# Uses Realtime Buttons to simulate the controls for a robot
# Rather than sending a single click when a button is clicked, Realtime Buttons
# send button presses continuously while the button is pressed down.
# Two examples, one using fancy graphics, one plain.
def RemoteControlExample():
# Make a form, but don't use context manager
sg.set_options(element_padding=(0,0))
back = '#eeeeee'
2018-09-08 03:23:45 +00:00
sg.set_options(border_width=0,
button_color=('black', back),
background_color=back,
element_background_color=back,
text_element_background_color=back)
2018-09-08 03:23:45 +00:00
mypad = ((50,0),0)
layout = [[sg.Text('Robotics Remote Control')],
[sg.Text('', justification='center', size=(19,1), key='status')],
[ sg.RealtimeButton('', key='Forward', pad=mypad)],
[ sg.RealtimeButton('', key='Left', ),
sg.RealtimeButton('', key='Right', pad=mypad)],
[ sg.RealtimeButton('', key='Reverse', pad=mypad)],
[sg.Text('')],
[sg.Quit(button_color=('black', 'orange'))]]
2018-07-31 14:53:55 +00:00
window = sg.Window('Robotics Remote Control', layout, grab_anywhere=False)
2018-07-31 14:53:55 +00:00
#
# Some place later in your code...
# You need to perform a ReadNonBlocking on your form every now and then or
# else it won't refresh.
#
# your program's main loop
while True:
2018-07-31 14:53:55 +00:00
# This is the code that reads and updates your window
event, values = window.read(timeout=0, timeout_key='timeout')
if event is not None:
window['status'].update(event)
2018-10-29 00:01:03 +00:00
elif event != 'timeout':
window['status'].update('')
2018-07-31 14:53:55 +00:00
# if user clicked quit button OR closed the form using the X, then break out of loop
if event == 'Quit' or values is None:
2018-07-31 14:53:55 +00:00
break
window.close()
2018-07-31 14:53:55 +00:00
def RemoteControlExample_NoGraphics():
# Make a form, but don't use context manager
2018-09-08 03:23:45 +00:00
layout = [[sg.Text('Robotics Remote Control', justification='center')],
[sg.Text('', justification='center', size=(19,1), key='status')],
[sg.Text(' '*8), sg.RealtimeButton('Forward')],
[ sg.RealtimeButton('Left'), sg.Text(' '), sg.RealtimeButton('Right')],
[sg.Text(' '*8), sg.RealtimeButton('Reverse')],
[sg.Text('')],
[sg.Quit(button_color=('black', 'orange'))]]
2018-07-31 14:53:55 +00:00
# Display form to user
window = sg.Window('Robotics Remote Control', layout, grab_anywhere=False)
2018-07-31 14:53:55 +00:00
#
# Some place later in your code...
2018-10-29 00:01:03 +00:00
# You need to perform a Read on your form every now and then or
2018-07-31 14:53:55 +00:00
# else it won't refresh.
2018-10-29 00:01:03 +00:00
# Notice how the timeout is 100ms. You don't have to use a timeout = 0 for all of your hardware
# applications. Leave some CPU for other threads or for your GUI. The longer you are in the GUI, the more
# responsive the GUI itself will be Match your timeout with your hardware's capabilities
2018-07-31 14:53:55 +00:00
#
# your program's main loop
while True :
2018-07-31 14:53:55 +00:00
# This is the code that reads and updates your window
event, values = window.read(timeout=100, timeout_key='timeout')
2018-10-29 00:01:03 +00:00
# print(event, values)
if event != 'timeout':
window['status'].update(event)
2018-07-31 14:53:55 +00:00
else:
window['status'].update('')
2018-07-31 14:53:55 +00:00
# if user clicked quit button OR closed the form using the X, then break out of loop
if event in (sg.WIN_CLOSED, 'Quit'):
2018-07-31 14:53:55 +00:00
break
window.close()
2018-07-31 14:53:55 +00:00
# ------------------------------------- main -------------------------------------
def main():
RemoteControlExample_NoGraphics()
# Uncomment to get the fancy graphics version. Be sure and download the button images!
2018-08-09 17:18:04 +00:00
RemoteControlExample()
# sg.popup('End of non-blocking demonstration')
2018-07-31 14:53:55 +00:00
if __name__ == '__main__':
main()