2019-04-04 18:39:02 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2019-10-23 20:10:03 +00:00
|
|
|
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-10-23 20:10:03 +00:00
|
|
|
|
2019-04-04 18:39:02 +00:00
|
|
|
def callback_function1():
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.popup('In Callback Function 1')
|
2019-04-04 18:39:02 +00:00
|
|
|
print('In the callback function 1')
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2019-04-04 18:39:02 +00:00
|
|
|
def callback_function2():
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.popup('In Callback Function 2')
|
2019-04-04 18:39:02 +00:00
|
|
|
print('In the callback function 2')
|
|
|
|
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
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
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2020-05-07 10:22:59 +00:00
|
|
|
if event == sg.WIN_CLOSED:
|
2019-04-04 18:39:02 +00:00
|
|
|
break
|
|
|
|
elif event == 'Button 1':
|
|
|
|
callback_function1() # call the "Callback" function
|
|
|
|
elif event == 'Button 2':
|
|
|
|
callback_function2() # call the "Callback" function
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|