Skip to content

Tutorials

vmagnin edited this page May 11, 2022 · 50 revisions

Having built and installed gtk-fortran following the wiki instructions, you now need tutorials and how-to to learn programming with gtk-fortran.

Our tutorials & how-to

Prerequisites

  • As you have built the gtk-fortran library, we can already suppose you have a modern Fortran compiler implementing the iso_c_binding module (Fortran 2003) and some Fortran 2008 functions. In those tutorials, we will use gfortran. See the Fortran-lang.org compilers page for other compilers.
  • You need pkg-config (or pkgconf) because GTK is composed of many libraries. Such a tool will make easier writing the compilation command.
  • You need a text editor and a terminal.
  • You need to be familiar with Fortran basics. If you are not, the Fortran-lang community offers good tutorials: https://fortran-lang.org/learn/
  • Being familiar with the C language is not compulsory but having some knowledge of the C types and especially C pointers and functions prototypes will help you understanding the GTK documentation and the gtk-fortran programming.
  • Most tutorials are for GTK 4, but with the GTK 3 modifications given.

Tutorials & how-to list

More resources for learning gtk-fortran

The gtk-fortran examples

You can begin by studying the examples available in the examples/ directory, 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. You can even simply create a PNG file without creating any GUI, by using the GdkPixbuf library: see pixbuf_without_gui.f90.

Some external docs about gtk-fortran

GTK resources

GtkInspector

GtkInspector is an incredible interactive debugging tool for your GTK applications. Type:

$ gsettings set org.gtk.Settings.Debug enable-inspector-keybinding true

then launch your GTK application. Put the focus on it and press CTRL+SHIFT+D to launch the GtkInspector (or CTRL+SHIFT+I to directly inspect the widget under the mouse cursor).

You can see and edit on the fly the properties of each widget, you can change the graphical theme, you can zoom on a widget, edit the CSS properties, and many other things.

See that introduction tutorial: https://blog.gtk.org/tag/inspector/

and those Michael B.'s videos:

GTK tutorials

Some good blogs to learn GTK:

You can also launch gtk4-demo to run the official GTK examples and read their source code.

Books

Books about GTK are rare. There is especially the Andrew Krause's book: the first one is about GTK 2 (excellent but now obsolete, oriented toward the C language), the second one is the GTK 3 edition but for Python:

GTK links

French ressources - Liens sur GTK et Fortran en Français

Clone this wiki locally