This example places a button and a label into a window. To do this, you have to use a placement box.
Placement boxes are available in two flavours:
In this example a vertical placement box is used.
Archive file with this example: Bttn-Example002.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 :: Label -> IO () buttonClickHandler lbl = do labelSetText lbl "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" label <- labelNew (Just "0") box <- vBoxNew True 4 -- the properties of the main window are set set window [windowDefaultWidth := 250, windowDefaultHeight := 200, widgetWidthRequest := 100, widgetHeightRequest := 80, windowWindowPosition := WinPosCenter, containerChild := box] -- the placement of the widgets is done here boxPackStart box button PackGrow 0 boxPackStart box label PackGrow 0 -- all needed callback functions are provided on button buttonActivated (buttonClickHandler label) on window objectDestroy destroyEventHandler -- all widgets are made visible widgetShow button widgetShow label widgetShow box widgetShow window -- the event processing is started mainGUI
It is possible to write the button click handler in a slightly different form:
buttonClickHandler :: (LabelClass lc) => lc -> IO () buttonClickHandler lbl = do labelSetText lbl "Hi"
Here we use a class constraint to specify the type of the handler's first parameter.
A program version with this code can be found in Bttn-Example002-bis.tar.gz.