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

Playing with Buttons, Labels and Placement Elements

A Window with a Single Button


This window contains a single button. The button fills the entire window. It is labelled "Click here". When it is clicked the first time, the button label changes to "Hi".

The window before the button was clicked

The window after the button was clicked

Archive file with this example: Bttn-Example001.tar.gz. Be sure to read the file README.txt for further instructions.

import "gtk3" Graphics.UI.Gtk

destroyEventHandler :: IO ()
destroyEventHandler =
  do mainQuit

buttonClickHandler :: Button  -> IO ()
buttonClickHandler bttn  =
  do
    buttonSetLabel bttn "Hi"

main :: IO ()
main = 
  do
    initGUI
    window <- windowNew
    button <- buttonNewWithLabel "Click here"
    set window [windowDefaultWidth := 250,
                windowDefaultHeight := 200,
                windowWindowPosition := WinPosCenter,
                containerChild := button]
    on button buttonActivated (buttonClickHandler button)
    on window objectDestroy destroyEventHandler
    widgetShow button
    widgetShow window
    mainGUI

To enable your program to process a button click, you have to do two things:

The button handler is not difficult to understand:

It assigns a new label text to the button.

The connection between the button and its event handler is established with the statement:

on button buttonActivated (buttonClickHandler button)

Note that

(buttonClickHandler button)

is a function call that returns a parameterless function with result type:

IO ()

This is due to currying.


previous page Table of Section Contents next page