zu www.bildungsgueter.de next page Table of Section Contents

First Window


It is not difficult at all to programm a window. The creation of a window requires only four function calls that can be written into one single function definition:

import "gtk3" Graphics.UI.Gtk

main :: IO ()
main = do
    initGUI
    window <- windowNew
    widgetShow window
    mainGUI

This is sufficient to create and to display a window. The window has no content at all, but it is decorated with a title bar and a frame. The title bar provides facilities to move, to resize, to minimize, to maximize and to close the window. Some window managers may offer additional actions.

Explanations and Notes

Archive file with this example: Example001.tar.gz. Be sure to read the file README.txt for further instructions. Included in that archive file is the makefile that is described below.


Compilation

To compile a program you can directly call the ghc compiler:

ghc -O2 -o Main Main.hs -XPackageImports

The compiler creates three files:

It is also possible to prepare a small makefile to simplify the compilation:

CC       = ghc
NAME     = Main
OPTIONS  =  -XPackageImports

all: $(NAME).hs
	$(CC) -O2 -o $(NAME) $(NAME).hs $(OPTIONS)

clean:
	rm -f $(NAME).hi
	rm -f $(NAME).o
	rm -f $(NAME)

The overall structure of this makefile follows established conventions:

Note that make targets are written with tabs. In the text shown above, a green bar indicates the presence of a tab. These tabs are in fact needed; the makefile will fail to work when you replace the tabs with a sequence of blanks! You can copy the text shown above into your preferred text editor. In principle the copy function can keep the tabs, but bear in mind that you have to configure your editor in such a way that it does not automatically expand tabs into sequences of blanks.

This tutorial provides makefiles for all examples.


To compile your program with that makefile, open a termial window in the foulder that contains your program and the makefile and type:

make

To start the compiled program, enter

./Main

into the terminal window you used to compile the program.

Regrettably, the code shown is not sufficient to terminate the program after the window was removed from the screen.

To see that the program continues to run after its window was closed, do the following:


Table of Section Contents next page