October 21, 2022
Writing GUI apps with the c language
September 18, 2022
Creating GUI apps in Linux
A common question for programmer newbies is to ask which of the existing GUI frameworks is the most widespread used, or the easiest one to learn. Other users may ask which programming language might fits well for writing large scale GUI apps. The surprising situation is, that none of the existing libraries like Qt, tkinter or WXwidget can be recommended and all the mainstream languages like Python, Java and C++ are an antipattern to develop a GUI app.
With this forward it might be interesting to know what the preferred choice is in writing a GUI app. The working hypothesis is, that not a certain programming language is important but a certain GUI builder software. A gui builder is a WYSIWYG program which allows to drag and drop a GUI interface similar to painting an image, and then the code is generated by the program.
Let us take a closer look what very different software tools like MS-Access, Borland C++ Builder, code:blocks and WXglade have in common. In all these environments, the user isn't asked to write a single line of code, but he has menu with widgets like a button, a textfield and a combo box and then the tool knows how to write the code.
The reason why a gui builder is not optional but mandatory is because a GUI framework like Qt or any other one is a large scale library which contains of thousands of classes. It is not possible to use a small gui library. Even so called lightweight GUI libraries like FLTK will occupy around 4 MB on the hard drive and provides endless amount of options and parameters. The only way to master all the widget is an extra tool which is not working with sourcecode but with a graphical interface.
Let us take a closer look into a smaller but very powerful GUI builder which is wxglade. The amount of features is very low. The user sees in the left pane the list of widget and is asked to drag and drop them into the right pane. The interesting situation is, that in setting menu for the entire application it is possible to specify a concrete programming language:

The menu looks not very impressive,, it is only a dialogue box with the option for C++, python, Perl and Lisp. The interesting situation is, that at least the wxglade software knows how to convert a GUI window into runnable sourcecode.
Let us investigate what will happen after activating the "C++" option. The wxglade software is producing the .cpp and the .h files. Also it is written in the preface in the comment section how to compile the .cpp code into a binary file. The user has to enter the command into the terminal and gets the application. A simple example Hello world windows contains of a text widget and a button occupies around 164 kb in total and can be executed with "./MyApp". It is a normal C++ program written by a machine.
After creating the code, the human programmer is asked to improve the generated code. He can add some events or program the database backend by itself. The interesting situation is, that such a workflow is much easier than writing the app from scratch. The assumption is, that 100% of the C++ GUI apps are written in this programming style. In all the cases, the beginning is always a GUI Builder which produces some code, and this code is improved more or less by a human programmer into an application.
This pipeline makes sense because a simple form which contains of only 3 buttons and some widgets will result into a long class which has 200 lines of code. It is not possible or very complicated to write the code by hand only with a printed manual. This is only possible if the programmer knows already what a concrete programming language and a concrete GUI framework has to offer.
Perhaps it should be mentioned, that all the generated code from Wxglade is using the Wxwidget. Somebody may argue that wxwidget is not the best framework and this assumption is correct. But, suppose a different gui builder will use a different framework, for example Qt. Then the pipeline is the same. At first the programmer starts a GUI builder, the code is generated by the software and then the programmer improves the code a bit or more. So we can say the focus is always on a gui builder which stands in the center and a concrete programming language or a concrete GUI library is less important.
In case of GUI programming the question is not if C++ or Python is the better choice. And the question is not if Wxwidget or Qt makes sense. But the more serious question is, if Visual studio, wxglade or Eclipse is a here to stay. GUIs are developed with a certain GUI builder, the gui builder defines how the sourcecode will look like.
There is some sort of TIOBE Index for gui builder tools. Popular tools are visual studio, eclipse and android studio These programs are not used because of syntax highlighting or other text editor capabilities but the dominant reason why programmers are using Visual studio is because of the GUI builder functionality. It allows them to draw an application similar to draw a picture with a graphics program.
GUI builders
The most interesting feature of a GUI Builder is, that it interprets the world from a certain perspective. There is no need to decide for or against a programming language. For a gui builder the difference between C++ and is low, even both languages are very different from each other. The reason is that a gui builder doesn't asks the user to type in sourcecode, but the user has drag & drop widgets and press the "generate" button.
The reason why most GUI apps are developed for Windows is because the visual studio software is a mature one which is over 20 years old and its core component is the mentioned gui builder. Alternative tools like wxglade have less features and are less well documented.
The interesting situation with visual studio is that the software can be used to create C++ and C# code as well. So the question is not which of the languages is the better one, but the GUI builder works fine with both.
Even if GUI builders are highly important in software development they are seldom discussed in the literature. The assumption in the past was that it depends on the individual choice of a programmer if and which gui builder he prefers. Also the tools are nearly invisible in the final sourcecode, because what is available as sourcecode is written in a certain programming like C++ or java. So the assumption was, to focus on the language or maybe on the GUI library but not on the tool which is responsible for the code.
[1] https://visualstudiomagazine.com/articles/2017/08/15/ide-ranking.aspx
August 28, 2022
Creating GUI applications with wxpython
GUI Builder
MVC principle
References
November 28, 2019
GUI development with Linux
A possible alternative would be to use Python and GTK to create a native Linux desktop GUI app. The problem with gtk under Python is, that the amount of documentation is low, i have tried out to create a simple hello world gui and failed. The problem is not completely new because the combination of C++ and GTK is also hard to master. The reason why is hard to explain, but what is for sure is, that python+tkinter is much easier to master.
Let us take a look what Windows programmers are doing. The normal way in creating a GUI app goes over the C# language which comes with a strong GUI library. Creating a C# GUI app in Windows is as easy as with tkinter in Python. The difference is, that a C# program can be used for productive code, while Python is some kind of prototyping language.
The hypothesis is, that right now, no good GUI frameworks are available for Linux. In the normal C++ language no GUI library is available and using GTK is too complicated. It seems, that from a os development side it's complicated to create and document a GUI framework which is used by normal programmers. A naive approach would be to use the C# language in Linux as well, but unfortunately, the forms library will work only in Windows environments.
The reason why Linux desktop are not equipped with a more elaborated GTK framework is simple. It's part of the Anti-RHEL conspiracy. Microsoft, Adobe and all the major computer journalists are boycotting the Linux ecosystem and the small Red Hat company is not strong enough to build it's own GUI framework. THe problem is, that GUI toolkits are patent protected and it's not allowed to develop an easy to use framework from scratch. In theory, Red Hat can develop a more elaborated GTK toolkit which is easy to use from Python and C++ but as a consequence Microsoft would start a legal conflict against Linux and the result is, that no Linux operating system at all will be available anymore.
The simple advice for programers is to stay within the Microsoft environment. It's the more easy to use operating system for GUI development. C# under Windows can be seen as the best practice method in programming a GUI interface. As a consequence many native Windows applications were developed by the programmers while under Linux only textual programs are available.
September 25, 2019
Pyglet hello world program
It's unsure, if pyglet can replace the famous pygame library. Because it's very new, and the information are spread over different stackoverflow entries. For testing out the capabilities, a small hello world program make sense, which draws a line, a sprite and asks for keyboard input. From the performance side, it's much better than pygame. The window consums nearly zero CPU ressources, it's on the same level like a game written in fast C++ with the SFML library. A short look into the sourcecode shows, that pyglet is a bit tricky to use. The first thing which is important is to inherit a window, and secondly, the manipulation of the colors is complicated too. The hope is, that in larger programs, it will become easier to use, because the drawing primitives can be capsuled in subfunctions. What we can say for sure, is that the program written in Python is much easier to write, than the same App written in C++, Java or C.

#!/usr/bin/env python3
import pyglet
from pyglet.window import key
class GameWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pos=(200,200)
self.angle=0
self.movement=20
imagetrain = pyglet.resource.image('locomotive.png')
self.spritetrain = pyglet.sprite.Sprite(imagetrain)
self.label = pyglet.text.Label('Hello, world',x=100,y=100,color=(100,100,255,255))
def on_mouse_press(self, x, y, button, modifiers):
pass
def on_mouse_release(self, x, y, button, modifiers):
pass
def on_mouse_motion(self, x, y, dx, dy):
pass
#print("move mouse")
def on_key_press(self, symbol, modifiers):
#print("key was pressed",symbol)
if symbol == key.A: self.angle-=10
elif symbol == key.B: self.angle+=10
elif symbol == key.ENTER: pass
elif symbol == key.LEFT:
self.pos=(self.pos[0]-self.movement,self.pos[1])
elif symbol == key.RIGHT:
self.pos=(self.pos[0]+self.movement,self.pos[1])
elif symbol == key.UP:
self.pos=(self.pos[0],self.pos[1]+self.movement)
elif symbol == key.DOWN:
self.pos=(self.pos[0],self.pos[1]-self.movement)
self.spritetrain.update(x=self.pos[0],y=self.pos[1],rotation=self.angle)
def update(self, dt):
pass
def on_draw(self):
self.clear()
pyglet.gl.glClearColor(1,1,1,1)
pyglet.graphics.draw(2,pyglet.gl.GL_LINES,
('v2i', (10, 15, 130, 135)),
('c3B', (100,100,255)*2), # one color per vertex
)
self.spritetrain.draw()
self.label.draw()
if __name__ == "__main__":
window = GameWindow(600, 400, "pyglet",resizable=False)
pyglet.clock.schedule_interval(window.update, 1/30.0)
pyglet.app.run()
Update
In a second version of the program, a dedicated class for the physics state was created. This simplified the programming a bit. The remaining commands for drawing the lines and the sprites will become easier, because once the code is written, it will draw all the elements to the screen.
Secondly, it was measured the exact cpu consumption. For 100 frames per second, the GUI app needs 15% of the totai CPU.
ps -p 31339 -o %cpu,%mem,cmd %CPU %MEM CMD 15.1 1.3 python3 1.py
If the framerate is increased to 200 fps, the cpu consumption is 18%. It's important to know, that the only objects on the screen, are one sprite, one line and a small “hello world” text. It seems, that Python programs in general are running slow? No, that is not the case. Because a program written in high speed C++ with the SFML library has the same performance problems. It seems, that the requirements of updating the screen 200 times per second is too much for computers in general. No matter in which programming language the code was written.
July 24, 2018
Creating a GUI with C++, gtkmm and glade
// Glade gui with gtkmm
// g++ -std=c++14 `pkg-config --cflags --libs gtkmm-3.0`main.cpp
// https://prognotes.net/2015/06/gtk-3-c-program-using-glade-3/
#include iostream
#include gtkmm.h
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkBuilder *builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "gui.glade", NULL);
GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "mainwindow")); // from XML file
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}




