PySimpleGUI/DemoPrograms/Demo_Button_Func_Calls.py

43 lines
1.2 KiB
Python
Raw Normal View History

2019-04-04 18:39:02 +00:00
#!/usr/bin/env python
import sys
import PySimpleGUI as sg
2019-04-04 18:39:02 +00:00
"""
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
"""
2019-04-04 18:39:02 +00:00
def callback_function1():
sg.popup('In Callback Function 1')
2019-04-04 18:39:02 +00:00
print('In the callback function 1')
2019-04-04 18:39:02 +00:00
def callback_function2():
sg.popup('In Callback Function 2')
2019-04-04 18:39:02 +00:00
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)
2019-04-04 18:39:02 +00:00
while True: # Event Loop
event, values = window.read()
2019-04-04 18:39:02 +00:00
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()