”;
Any GUI application is event driven, having the ability to respond to the various possible events occurring on the GUI elements. In PySimpleGUI, the event handling is done inside an infinite loop below the constitution of GUI design, continuously checking whether an event occurs and perform the actions based on the event.
There are two types of events −
-
Window events, and
-
Element events.
The window events are enabled by default, and include the button events (occur when any button is clicked) and the event of the “X” button on the titlebar clicked.
The element events are not enabled by default. Element-specific events can be detected only when the “enable_events” parameter is set to True when an element is created.
Window Closed Event
The infinite event loop that makes the PySimpleGUI window persistent, is terminated when the user presses the “X” button, or the close() method of Window class is executed. The standard way of terminating the loop is as follows −
import PySimpleGUI as psg ... while True: ... if event == psg.WIN_CLOSED: break ... window.close()
The Widow class also emits an “enable_close_attempted_event” if this parameter is set to True. It is a good practice to call yes-no popup when it is detected inside the loop.
window = psg.Window(''Calculator'', layout, enable_close_attempted_event=True) while True: event, values = window.read() print(event, values) if event == "Add": result = int(values[''-FIRST-'']) + int(values[''-SECOND-'']) if event == "Sub": result = int(values[''-FIRST-'']) - int(values[''-SECOND-'']) window[''-OUT-''].update(result) if event == psg.WINDOW_CLOSE_ATTEMPTED_EVENT and psg.popup_yes_no(''Do you really want to exit?'') == ''Yes'': break if event == psg.WIN_CLOSED or event == ''Exit'': break
In this case, as the “X” button is pressed, the Popup with Yes/No button appears and the program exits when the “Yes” button is clicked.
It will produce the following output window −
The event value also returns the “-WINDOW CLOSE ATTEMPTED-” value.
-WINDOW CLOSE ATTEMPTED- {''-FIRST-'': ''200'', ''-SECOND-'': ''300''}
Button Events
The button click event is enabled by default. To disable, use “Button.update(disabled=True)”. You can also set “enable_events=True” in Button’s constructor, it will enable the Button Modified event. This event is triggered when something ”writes” to a button.
When we read the contents of the window (using “window.read()”), the button value will be either its caption (if key is not set) or key if it is set.
In the above example, since the key parameter is not set on the Add and Sub buttons, their captions are returned when the window is read.
Add {''-FIRST-'': ''200'', ''-SECOND-'': ''300''}
Add key parameters to Add and Sub buttons in the program.
import PySimpleGUI as psg layout = [ [psg.Text(''Enter a num: ''), psg.Input(key=''-FIRST-'')], [psg.Text(''Enter a num: ''), psg.Input(key=''-SECOND-'')], [psg.Text(''Result : ''), psg.Text(key=''-OUT-'')], [psg.Button("Add", key=''-ADD-''), psg.Button("Sub", key=''- SUB-''), psg.Exit()], ] window = psg.Window(''Calculator'', layout) while True: event, values = window.read() print(event, values) if event == "-ADD-": result = int(values[''-FIRST-'']) + int(values[''-SECOND-'']) if event == "-SUB-": result = int(values[''-FIRST-'']) - int(values[''-SECOND-'']) window[''-OUT-''].update(result) if event == psg.WIN_CLOSED or event == ''Exit'': break window.close()
The tuple returned by the read() method will now show the key of button pressed.
-ADD- {''-FIRST-'': ''200'', ''-SECOND-'': ''300''}
Events of Other Elements
Many of the elements emit events when some type of user interaction takes place. For example, when a slider is moved, or an item from the list is selected on or a radio button is clicked on.
Unlike Button or Window, these events are not enabled by default. To enable events for an Element, set the parameter “enable_events=True”.
The following table shows the elements and the events generated by them.
Name | Events |
---|---|
InputText | any key pressed |
Combo | item selected |
Listbox | selection changed |
Radio | selection changed |
Checkbox | selection changed |
Spinner | new item selected |
Multiline | any key pressed |
Text | Clicked |
Status Bar | Clicked |
Graph | Clicked |
Graph | Dragged |
Graph | drag ended (mouse up) |
TabGroup | tab clicked |
Slider | slider moved |
Table | row selected |
Tree | node selected |
ButtonMenu | menu item chosen |
Right click menu | menu item chosen |
”;