Skip to content

Learning from examples

vmagnin edited this page May 11, 2022 · 9 revisions

Once you will have created your first gtk-fortran toy applications, you will need to use more widgets and GTK advanced features. Learning by reading and playing with gtk-fortran and GTK examples is a good way.

The gtk-fortran examples

You can study the examples available in the examples/ and plplot/ directories, beginning by gtkzero_gapp.f90 (just creates an empty GTK window) and gtkhello.f90 (a window with two buttons). Those simple examples are heavily commented in order to help you learn the GTK basics.

Note that in GTK 4, the recommended way to write a GTK program is using GtkApplication like in the gtkzero_gapp.f90 example: in the gtk3 branch, it's the only example using GtkApplication. But if you use GTK 3, try to follow this example for your own programs in order to ease the porting to GTK 4.

If you want to draw progressively a picture pixel by pixel during a scientific computation, mandelbrot_pixbuf.f90 is a good starting point:

Mandelbrot set plotted with gtk-fortran on macOS

You can even simply create a PNG file without creating any GUI, by using the GdkPixbuf library: see pixbuf_without_gui.f90.

You may also be interested by the gtk-fortran-extra repository which offers more examples (under MIT license), especially demonstrating how you can obtain an application that can be built with or without its GUI.

The GTK examples

You can also run the gtk4-demo command (gtk-4-examples package in Ubuntu) to launch each official GTK example and read directly its C source code.

Clone this wiki locally