diff --git a/DemoPrograms/Demo_Buttons_Realtime.py b/DemoPrograms/Demo_Buttons_Realtime.py new file mode 100644 index 00000000..a3b0b4b5 --- /dev/null +++ b/DemoPrograms/Demo_Buttons_Realtime.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +""" + Demo - Realtime Buttons + + Realtime buttons provide a way for you to get a continuous stream of button + events for as long as a button is held down. + + This demo is using a timeout to determine that a button has been released. + If your application doesn't care when a button is released and only needs to know + that it's being held down, then you can remove the timeout on the window read call. + + Note that your reaction latency will be the same as your timeout value. In this demo + the timeout is 100, so there will be 100ms between releasing a button and your program detecting + this has happened. + + Copyright 2021 PySimpleGUI +""" + +import PySimpleGUI as sg + +def main(): + # The Quit button is being placed in the bottom right corner and the colors are inverted, just for fun + layout = [[sg.Text('Robotics Remote Control')], + [sg.Text('Hold Down Button To Move')], + [sg.Text()], + [sg.Text(' '), + sg.RealtimeButton(sg.SYMBOL_UP, key='-FORWARD-')], + [sg.RealtimeButton(sg.SYMBOL_LEFT, key='-LEFT-'), + sg.Text(size=(10,1), key='-STATUS-', justification='c', pad=(0,0)), + sg.RealtimeButton(sg.SYMBOL_RIGHT, key='-RIGHT-')], + [sg.Text(' '), + sg.RealtimeButton(sg.SYMBOL_DOWN, key='-DOWN-')], + [sg.Text()], + [sg.Column([[sg.Quit(button_color=(sg.theme_button_color()[1], sg.theme_button_color()[0]), focus=True)]], justification='r')]] + + window = sg.Window('Robotics Remote Control', layout) + + while True: + # This is the code that reads and updates your window + event, values = window.read(timeout=100) + if event in (sg.WIN_CLOSED, 'Quit'): + break + if event != sg.TIMEOUT_EVENT: + # if not a timeout event, then it's a button that's being held down + window['-STATUS-'].update(event) + else: + # A timeout signals that all buttons have been released so clear the status display + window['-STATUS-'].update('') + + window.close() + +if __name__ == '__main__': + sg.theme('dark red') + main() diff --git a/DemoPrograms/Demo_Pi_Robotics.py b/DemoPrograms/Demo_Pi_Robotics.py deleted file mode 100644 index a1ec9d3a..00000000 --- a/DemoPrograms/Demo_Pi_Robotics.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python -import sys -import PySimpleGUI as sg - - -# 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' - - sg.set_options(border_width=0, - button_color=('black', back), - background_color=back, - element_background_color=back, - text_element_background_color=back) - - 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'))]] - - window = sg.Window('Robotics Remote Control', layout, grab_anywhere=False) - - # - # 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: - # 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) - elif event != 'timeout': - window['status'].update('') - # if user clicked quit button OR closed the form using the X, then break out of loop - if event == 'Quit' or values is None: - break - - window.close() - - -def RemoteControlExample_NoGraphics(): - # Make a form, but don't use context manager - - 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'))]] - # Display form to user - window = sg.Window('Robotics Remote Control', layout, grab_anywhere=False) - - # - # Some place later in your code... - # You need to perform a Read on your form every now and then or - # else it won't refresh. - # 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 - # - # your program's main loop - while True : - # This is the code that reads and updates your window - event, values = window.read(timeout=100, timeout_key='timeout') - # print(event, values) - if event != 'timeout': - window['status'].update(event) - else: - window['status'].update('') - # if user clicked quit button OR closed the form using the X, then break out of loop - if event in (sg.WIN_CLOSED, 'Quit'): - break - - window.close() - -# ------------------------------------- main ------------------------------------- -def main(): - RemoteControlExample_NoGraphics() - # Uncomment to get the fancy graphics version. Be sure and download the button images! - RemoteControlExample() - # sg.popup('End of non-blocking demonstration') - -if __name__ == '__main__': - - main()