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".
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 -- The destroy event for the main window is handled here. -- The handler terminates the Gtk event processing. destroyEventHandler :: IO () destroyEventHandler = do mainQuit -- The buttonActivated event of the push button is -- handled here. The handler changes the button label -- of the button to indicate that the button was pressed. buttonClickHandler :: Button -> IO () buttonClickHandler bttn = do buttonSetLabel bttn "Hi" -- This is the main program. -- The main program creates a window, provides some -- window content and makes the window visible. -- Finally, the main program starts the event processing -- of Gtk-3. main :: IO () main = do initGUI -- all needed widgets are created window <- windowNew button <- buttonNewWithLabel "Click here" -- the properties of the main window are set set window [windowDefaultWidth := 250, windowDefaultHeight := 200, widgetWidthRequest := 100, widgetHeightRequest := 80, windowWindowPosition := WinPosCenter, containerChild := button] -- all needed callback functions are provided on button buttonActivated (buttonClickHandler button) on window objectDestroy destroyEventHandler -- all widgets are made visible widgetShow button widgetShow window -- the event processing is started 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.