August 28, 2022

Creating GUI applications with wxpython

GUI Builder

The good news is, that all the tools are available in the Linux operating system to create powerful GUI programs. The disadvantage is, that the documentation is spreaded over many places and it is difficult to get an overview. The assumption is, that the reader is familiar with the Python programming language already and likes to create a full blow GUI desktop application. The tools which are needed are: wxpython library and the wxglade GUI builder.
The main problem with wxpython is, that the API contains endless amount of widgets and config parameters. The official API documentation is very large and reading all the material is not possible. The easier approach to create a window is a GUI builder. A GUI builder like wxglade allows the user to drag and drop the widgets into the frame. Such a gui builder is a must have tool, because otherwise the user has to enter the commands by hand into a programming editor. And here is the problem located which is visible for GUI programming in general. How does the user know which widget is created is created with which command? Right, he can't know because it is written in the documentation. But, searching for the correct statement in the docs takes time.
Let me give an example. Suppose the user likes to draw a text entry field. In the wxpython framework it is created with wx.TextCtrl(parameters). The reason why is because the API defines how to create such an element. Such command has nothing to do with programming in general but it is about using a certain GUI framework. The problem is that each GUI framework like tkinter wxwidget or Qt is using a different sort of commands. In addition, the parameterlist for all the commands is endless.
So the only option for the newbie is to the mentioned wxglade GUI builder. Here the user doesn't know the detail, but draws a window similar to create a PNG image in gimp. The GUI builder creates the code automatically which can be copied and pasted into the own application. A typical criticism against gui builders is that if the layout has changed the code has to updated manually. This concern is correct. It doesn't make sense to use a GUI builder for creating the code direct, but the GUI builder is a learning tool which allows to understand which commands are needed.
The user reads the automatically created code, and recognizes, that a certain widget was created with certain commands. This allows to get a faster overview over a widget. So the gui builder is the starting point to search for the details in the wxpython API documentation.
MVC principle
Another powerful concept to simplify the creation of a GUI app is the model view controller principle. MVC means to split a window into three different classes for the database backend, the layout and the events in the window. The layout contains of commands for creating the widgets. These commands are created with a gui builder or typed in manually. If the user is prressing on the buttons nothing will happen.
The event handling is done in another class which is the controller class. It is listen to the events and changes values into the form. If the action is more complicated the controller is using the backend class for additional processing.
Suppose the GUI app consists of 3 windows. Then the overall amount of classes is 9:
window1view
window1controller
window1model
window2view
window2controller
window2model
window3view
window3controller
window3model
 
References
[1] wxPython tutorial https://zetcode.com/wxpython/
[2] wxPython API Documentation https://docs.wxpython.org
[3] Youtube wxglade