From 8f6db74f8316220f31bb54f4fa1089f305f52be8 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 23 Sep 2019 12:10:10 -0400 Subject: [PATCH] New demo program that simulates callbacks using a dictionary --- .../Demo_Event_Callback_Simulation.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 DemoPrograms/Demo_Event_Callback_Simulation.py diff --git a/DemoPrograms/Demo_Event_Callback_Simulation.py b/DemoPrograms/Demo_Event_Callback_Simulation.py new file mode 100644 index 00000000..d3642368 --- /dev/null +++ b/DemoPrograms/Demo_Event_Callback_Simulation.py @@ -0,0 +1,53 @@ +import PySimpleGUI as sg +# import PySimpleGUIQt as sg +# import PySimpleGUIWx as sg + +''' + Event Callback Simulation + + This design pattern simulates callbacks for events. + This is NOT the "normal" way things work in PySimpleGUI and is an architecture that is actively discouraged + Unlike tkinter, Qt, etc, PySimpleGUI does not utilize callback + functions as a mechanism for communicating when button presses or other events happen. + BUT, should you want to quickly convert some existing code that does use callback functions, then this + is one way to do a "quick and dirty" port to PySimpleGUI. +''' + +# The callback functions +# These callbacks all display a message in a non-blocking way and immediately return +def button1(event, values): + sg.popup_quick_message('Button 1 callback', background_color='red', text_color='white') + +def button2(event, values): + sg.popup_quick_message('Button 2 callback', background_color='green', text_color='white') + +def catch_all(event, values): + sg.popup_quick_message(f'An unplanned event = "{event}" happend', background_color='blue', text_color='white', auto_close_duration=6) + +# Lookup dictionary that maps event to function to call. In this case, only 2 event have defined callbacks +func_dict = {'1':button1, '2':button2} + +# Layout the design of the GUI +layout = [[sg.Text('Please click a button')], + [sg.Button('1'), sg.Button('2'), sg.Button('Not defined', key='-MY-KEY-'), sg.Quit()]] + +# Show the Window to the user +window = sg.Window('Button callback example', layout) + +# Event loop. Read buttons, make callbacks +while True: + # Read the Window + event, values = window.read() + # Lookup event in function dictionary and call the function, passing in the event and values variables + try: + func_dict[event](event, values) # Call function with event and values + except: + catch_all(event, values) + # See if should close the window + if event in ('Quit', None): # normally this is done IMMEDIATELY after the read + break + +window.close() + +# All done! +sg.popup_auto_close('Done... this window auto closes') \ No newline at end of file