September 02, 2022

Writing GUI apps for Linux

 

GUI applications are a major concern in the Linux ecosystem. There are some frameworks available but none of them can be recommended. The following blogpost introduces the topic and compares the major options for programming window based applications.
Roughly spoken the list of gui frameworks consists of: Qt, gtk+, wxwidget, tkinter and FLTK. THe problem is, that the programmer can decide only for one of the frameworks. One the first look, Qt looks like a mature GUI framework. There are some points which are speaking against Qt. First thing is, that it is not only a C++ library but it extends the C++ language. Second problem is, that the amount of commits in the qt project is 5x lower than the amount of commits in the gnome project. And third point is, that larger LInux distributions like Ubuntu are preferring gnome.
Suppose gnome is a here to stay, the next question is how to create a gui. The first assumption is, that the underlying GTK+ library works pretty well to create guis. THe problem is, that gtk is documented poorly and even under python it is complicated to create a gui app. The better choice is to use the wxwidget framework.
Wxwidget looks the same, what PC users are using under Microsoft windows. It is full blown framework to create gui apps. It is working with python and C++ as well. The major concern against wxwidget is, that the size for an executable hello world app is too large. Indeed, a hello world app will need around 140 kb size in the working directory. And the app is doing nothing but showing a window with some pull down menus.
On the other hand it is possible to reduce the filesize with some tricks to around 22 kb:
g++ -Os -s helloworld.cpp `wx-config --libs --cxxflags`
upx a.out
Also it should be mentioned, that under the Windows operating system a much larger file size for hello world apps is common.
Compared to the wxwidget library, the FLTK library looks very outdated. Only a few tutorials are available and it seems, that the development has stopped around 2010. So the best choice for creating in the year 2022 a Linux gui app is wxwidget.
MVC concept
After deciding for one of the existing frameworks the next question is how to create the GUI app itself. Suppose the user has decided for the wxpython framework which allows to create GUI software in python, how exactly should the program look like? The major problem with GUI application is, that the inner principle is different from normal object oriented programming. OOP means basically to create objects for each elements. So the assumption of the newbie is, that he will needs different objects for the menu bar, the text box and the buttons. This is correct but wrong at the same time.
The major problem is gui programming is to connect these elements together. The end user will click on a button whichi activates the script and the script is searching something in the database. The problem is, that these process is executing functions in different parts of the program. So the question is how to arrange the classes.
MVC is a here to stay principle in solving the question Let us describe the concept step by step. The first file which has to be created is gui.py. This file holds the form which is displayed on the screen. It contains of the menu, the entry boxes and the buttons. Such a gui.py file can be created either with a text editor or with a gui designer like wxglade. After executing the file, the user will see the GUI on the screen which is a mockup. It won't do anything but show only the graphical frontend of the program.
In the second step two additional files are created which are controller.py and backend.py. The controller.py file contains of the event handling. It is the same what is known in Visual basic and MS-access as macros. For example, the user clicks on a button, and then the script in controller.py is executed. The script checks if the input is valid and decides which function should be called next. In the easiest case, the user is pressing on a button and then a “print()” statement is executed which displays on the command line that the button was pressed.
It is important to know that the macros, aka the eventhandling is never located in the gui.py file but it is outsourced into the controller class. This is the opposite of normal object oriented programming. Because of this reason the principle is called Model view controller principle. It is a technique how to write GUI code into different classes. The reason why MVC is used by 99% of the software is because it allows to create GUI apps much faster. The idea is, that in the first step only the GUI mockup itself is created. That means, the window on the screen has no functionality. The functionality is added later.