Package: psga
PySimpleGUI's Actions (PSGA) simplify event handlinginstallpython3-mpipinstallpsgaTo run the demo, checkout the repository and install the required modules:gitclonehttps://github.com/aptly-io/psga.gitcdpsga
python3-mvenv.venv
..venv/bin/activate
python3-mpipinstall-e.
python3-mpipinstall-e.[demo]pythondemos/tabs_and_tables/main.pyIntroPySimpleGUI is like theViewin theModel-View-Controllerparadigm.
For less complex user interfaces, PySimpleGui's typicalif-event then-action loop(that is a bit aController), works fine.
However the event-loop's if-then-else becomes difficult to maintain
for user interfaces with a large number of elements.PSGAmitigates this by adding the following:The@psga.action()decorator turns a python method (or function) in anAction.
This action wraps both the handler and an event name (accessible through the propertyname)
to respectively handle and name the PySimpleGui event.
Since VSC's intellisense recognizes thisnameproperty,
are typo errors in the PySimpleGui'skeyless likely to happen.
The optionalkeysdecorator parameter allows for additional keys that invoke the associated handler.AControllerclass groups and registers related handlers for processing
user interaction and updating the corresponding view.
This could hold your business/logic state.
With aControllerthe source code becomes more maintainable and structured.ADispatcherclass 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 itsController('s).
Manual registering is also possible (see the examples).It is easy to gradually refactor existing source code with thePSGAfeature.Note that PySimpleGUI's documentation mentions it avoids concepts likecall-backs,classes...PSGA, with itsactionandControllerbrings that back (so you might not likePSGA's concepts?).ExamplesAll examples are sprinkled with comments to highlightPSGA's usage.
Grep for# PSGA:to find these.Hello worldPySimpleGUI shows the classichello worldin itsJump-Start section.The source code below illustrates howPSGAcouldfit in thishello worldexample:Define a function that acts when theOkbutton is clicked.Instantiate the dispatcher that will trigger the handler whenever theOkevent is fired.Note that this simple example does not use aController.
Note that thedemos/hello_world.pyexample usagePSGAin a slightly different way
(all roads lead to Rome).importPySimpleGUIassgimportpsga# PSGA: define an action for the Ok button@psga.action(name="Ok")defon_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 Windowwindow=sg.Window('Window Title',layout)# PSGA: initiate the dispatcher, register the handler and process the window's eventspsga.Dispatcher().register(on_ok).loop(window)window.close()Example with a ControllerIt showsPSGA'sController,action()andDispatcherconcepts
(without introducing any PySimpleGUI functionality).frompsgaimportController,Dispatcher,actionclass_MyController(Controller):answer=0@action(name="universal_question")defon_ask(self,values):"""with explicit name"""_MyController.answer=values@action()defon_answer(self,values):"""with implicit name"""_MyController.answer=valuesdispatcher=Dispatcher()controller=_MyController(dispatcher)dispatcher.dispatch("universal_question",42)assertcontroller.answer==42QUESTION="What is the answer to the Ultimate Question of Life?"dispatcher.dispatch(controller.on_answer.name,QUESTION)assertcontroller.answer==QUESTIONTabs and tablesThedemo/tabs_and_tablesis a larger PySimpleGui example program.It shows 2sg.Tables, each in asg.Tab, that get their data from a Model.
The model in turn uses REST requests to manage the data.
Right-click for the context menu that allows to add or delete table rows.Notice howmain.pyis kept lean and clean.PSGAsimplifies the event processing in a single line of code:dispatcher.loop(window).
Each functional program part is succintly grouped in its Controller.For developmentFollowing illustrates how to setup forPSGAdevelopment.python3.11-mvenv.venv
..venv/bin/activate# install module's dependenciespipinstall-e.# optionally install test featurespipinstall-e.[test]# format, lint and test the codeisortdemostestssrc
blackdemostestssrc
pylintsrc
pytest# run the demosexportPYTHONPATH=src
pythondemos/hello_world.py
pythondemos/no_ui.py
pythondemos/tabs_and_tables/main.py# build the wheel and upload to pypi.org (uses credentials in ~/.pypirc)rm-rfdist/
python-mbuild
twinecheck--strictdist/*
twineuploaddist/*Note that on Mac OS one needs to install tkinter separately withbrew:brewinstallpython-tk@3.11