diff --git a/DemoPrograms/Demo_Event_Binding.py b/DemoPrograms/Demo_Event_Binding.py new file mode 100644 index 00000000..ac822956 --- /dev/null +++ b/DemoPrograms/Demo_Event_Binding.py @@ -0,0 +1,35 @@ +import PySimpleGUI as sg + +""" + Extending PySimpleGUI using the tkinter event bindings + + The idea here is to enable you to receive tkinter "Events" through the normal place you + get your events, the window.read() call. + + Both elements and windows have a bind method. + window.bind(tkinter_event_string, key) or element.bind(tkinter_event_string, key_modifier) + 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. +""" +sg.change_look_and_feel('Dark Blue 3') + +layout = [ [sg.Text('Move mouse over me', key='-TEXT-')], + [sg.In(key='-IN-')], + [sg.Button('Right Click Me', key='-BUTTON-'), sg.Button('Exit')] ] + +window = sg.Window('Window Title', layout, finalize=True) + +window.bind('', '+FOCUS OUT+') + +window['-BUTTON-'].bind('', '+RIGHT CLICK+') +window['-TEXT-'].bind('', '+MOUSE OVER+') +window['-TEXT-'].bind('', '+MOUSE AWAY+') +window['-IN-'].bind('', '+INPUT FOCUS+') + +while True: # Event Loop + event, values = window.read() + print(event, values) + if event in (None, 'Exit'): + break +window.close()