From da3176fb2022c4e45690f20e415d8fab81aece5e Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Thu, 4 Apr 2019 14:39:02 -0400 Subject: [PATCH] Initial Checkin of new callback demo --- DemoPrograms/Demo_Button_Func_Calls.py | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DemoPrograms/Demo_Button_Func_Calls.py diff --git a/DemoPrograms/Demo_Button_Func_Calls.py b/DemoPrograms/Demo_Button_Func_Calls.py new file mode 100644 index 00000000..c4d62a71 --- /dev/null +++ b/DemoPrograms/Demo_Button_Func_Calls.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +import sys +if sys.version_info[0] >= 3: + import PySimpleGUI as sg +else: + import PySimpleGUI27 as sg + + +""" +Demo Button Function Calls +Typically GUI packages in Python (tkinter, Qt, WxPython, etc) will call a user's function +when a button is clicked. This "Callback" model versus "Message Passing" model is a fundamental +difference between PySimpleGUI and all other GUI. + +There are NO BUTTON CALLBACKS in the PySimpleGUI Architecture + +It is quite easy to simulate these callbacks however. The way to do this is to add the calls +to your Event Loop +""" + +def callback_function1(): + sg.Popup('In Callback Function 1') + print('In the callback function 1') + +def callback_function2(): + sg.Popup('In Callback Function 2') + print('In the callback function 2') + +layout = [ [sg.Text('Demo of Button Callbacks')], + [sg.Button('Button 1'), sg.Button('Button 2')] ] + +window = sg.Window('Button Callback Simulation').Layout(layout) + +while True: # Event Loop + event, values = window.Read() + if event is None: + break + elif event == 'Button 1': + callback_function1() # call the "Callback" function + elif event == 'Button 2': + callback_function2() # call the "Callback" function + +window.Close()