Smarter event handling for PySimpleGUI
Project description
PySimpleGUI's Actions (PSGA) for smart event handling
Intro
PySimpleGUI is like the View in the Model-View-Controller paradigm. For less complex user interfaces its typical if-event-then-action loop (that takes the role of Controller) works fine. However this event-loop approach becomes unwieldy to maintain for larger user interfaces.
PSGA tries to mitigate this by adding the following:
- The
@psga.action()
decorator turns a method (or function) in anAction
. This action wraps both a handler and a event name (key) to respectively handle and name the event. - A
Controller
class groups and registers related handlers for processing user input and updating the corresponding view. Using controllers the source code becomes more maintainable and structured. - A
Dispatcher
class has a loop that reads the events from asg.Window
. Each event's value is then dispatched to the handler that was prior registered by theController
('s). Manual registering is also possible (see the example).
It is easy to gradually refactor existing source code with the PSGA feature. The Model of the MVC is still for the developer to design and implement.
Note:
- PySimpleGUI states in its documentation to get away with difficult concepts like call-backs, classes...
In a way,
action
andController
brings that back (so you might not like this). - While this module and python-tkinter have respectively a MIT or BSD license type, PySimpleGUI is LGPL.
Examples
Hello world
PySimpleGUI shows the classic hello world in its Jump-Start section.
The source code below illustrates how PSGA could fit in:
- Define a function that acts when the Ok button is clicked.
- Instantiate the dispatcher that triggers the handler when the Ok event is found.
Note that this simple example does not use a Controller
.
Note that the demos/hello_world.py
example does the same but slightly different.
import PySimpleGUI as sg
import psga
# PSGA: define an action for the Ok button
@psga.action(key="Ok")
def on_ok(values):
print('You entered ', values[0])
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.Text('Some text on Row 1')],
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Exit'), sg.Button('Ok')] ]
# Create the Window
window = sg.Window('Window Title', layout)
# PSGA: initiate the dispatcher, register the handler and process the window's events
psga.Dispatcher().register(on_ok).loop(window)
window.close()
Example with a Controller
It's not very functional (completely independent from PySimpleGUI).
But it illustrates the use of a Controller
in combination with
the action
decorator and Dispatcher
.
from psga import Controller, Dispatcher, action
class _MyController(Controller):
answer = 0
@action(key="universal_question")
def on_ask(self, values):
"""with explicit key"""
_MyController.answer = values
@action()
def on_answer(self, values):
"""with implicit key"""
_MyController.answer = values
dispatcher = Dispatcher()
controller = _MyController(dispatcher)
dispatcher.dispatch("universal_question", 42)
assert controller.answer == 42
QUESTION = "Answer to the Ultimate Question of Life"
dispatcher.dispatch(controller.on_answer.key, QUESTION)
assert controller.answer == QUESTION
For development
Illustrates how to setup for PSGA development.
python3.11 -mvenv .venv
. .venv/bin/activate
# install module's dependencies
pip install -e .
# optionally install test features
pip install -e .[test]
# format, lint and test the code
isort demos tests src
black demos tests src
pylint src
pytest
# run the demos
export PYTHONPATH=src
python demos/hello_world.py
python demos/no_ui.py
# build the wheel and upload to pypi.org (uses credentials in ~/.pypirc)
rm -rf dist/
python -m build
twine check --strict dist/*
twine upload dist/*
Note that on Mac OS one needs to install tkinter separately with brew:
brew install python-tk@3.11
brew install python-gdbm@3.11
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.