From 9d4d6d42cc84615efb18f0ecd7bc38b184eeb401 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 12 Mar 2021 13:07:57 -0500 Subject: [PATCH 1/2] New demo to show how to use 1 line of code to add a hotkey to your program --- DemoPrograms/Demo_Hotkey.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 DemoPrograms/Demo_Hotkey.py diff --git a/DemoPrograms/Demo_Hotkey.py b/DemoPrograms/Demo_Hotkey.py new file mode 100644 index 00000000..6dafcea1 --- /dev/null +++ b/DemoPrograms/Demo_Hotkey.py @@ -0,0 +1,31 @@ +import PySimpleGUI as sg +""" + Demo Hotkey + + Want a keyboard hotkey to cause your program to take some action + that's identical to a button being clicked? + + Well... that's 1 line of code that's needed. + + This line binds the F10 keybaord key to the window. It produces a "Go" event: + window.bind('', 'Go') + + Copyright 2021 PySimpleGUI +""" + +layout = [ [sg.Text('Press F10 to get same result as clicking "Go" button')], + [sg.Input(key='-IN-')], + [sg.Output(size=(30,8))], + [sg.Button('Go'), sg.Button('Exit')] ] + +window = sg.Window('Window Title', layout, finalize=True) + +window.bind('', 'Go') + +while True: # Event Loop + event, values = window.read() + print(event, values) + if event in (None, 'Exit'): + break + +window.close() \ No newline at end of file From 01af23431c39cafeb9ad66649a7b08b4845b09cd Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 12 Mar 2021 13:08:20 -0500 Subject: [PATCH 2/2] Added a window-level bind --- DemoPrograms/Demo_Event_Binding.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/DemoPrograms/Demo_Event_Binding.py b/DemoPrograms/Demo_Event_Binding.py index ba8f46f7..882d21af 100644 --- a/DemoPrograms/Demo_Event_Binding.py +++ b/DemoPrograms/Demo_Event_Binding.py @@ -11,15 +11,15 @@ import PySimpleGUI as sg First parameter is the tkinter event string. These are things like Second parameter for windows is an entire key, for elements is something added onto a key. This key or modified key is what is returned when you read the window. If the key modifier is text and the key is text, then the key returned from the read will be the 2 concatenated together. Otherwise your event will be a tuple containing the key_modifier value you pass in and the key belonging to the element the event happened to. + + Copyright 2021 PySimpleGUI """ sg.theme('Dark Blue 3') def main(): layout = [ [sg.Text('Move mouse over me', key='-TEXT-')], [sg.In(key='-IN-')], - [sg.Button('Right Click Me', key='-BUTTON-'), sg.Button('Right Click Me2', key=(2,3)),sg.Button('Exit'), - ] - ] + [sg.Button('Right Click Me', key='-BUTTON-'), sg.Button('Right Click Me2', key=(2,3)),sg.Button('Exit'),]] window = sg.Window('Window Title', layout, finalize=True) @@ -30,7 +30,7 @@ def main(): window['-TEXT-'].bind('', '+MOUSE OVER+') window['-TEXT-'].bind('', '+MOUSE AWAY+') window['-IN-'].bind('', '+INPUT FOCUS+') - + window.bind('', '* WINDOW ENTER *') while True: # Event Loop event, values = window.read() print(event, values) @@ -40,4 +40,4 @@ def main(): if __name__ == '__main__': - main() + main() \ No newline at end of file