Gtk2Hs is a GUI library for Haskell, based on GTK+, for creating graphical user interfaces. This page records some (trivial) notes about GUI programming in Haskell with the Gtk2Hs library.
Hello World with GTK
import Graphics.UI.Gtk main :: IO () main = do initGUI window <- windowNew widgetShowAll window -- onDestroy window mainQuit onDelete window (const (mainQuit >> return True)) mainGUI
initGUI must be called once before any other Gtk2Hs functions.
completes the internal steps and allocations, and show
window, as well as its children.
mainGUI is the infinite event loop, and event loop will block until the window get destroyed
since there’s a callback
onDelete event of the
The Gtk main event loop can be put into another separate OS thread. In such scenarios, something
MVar may be involved to synchronize the Gtk thread and the main thread of the program.
In GTK+, events are called signals. The object, event and event handler can be connected with
on :: object -> Signal object callback -> callback -> IO (ConnectId object) -- Defined in ‘glib-0.13.6.0:System.Glib.Signals’
after has the same type signature and functionality with
on, except that the
callback is executed after Gtk’s default handler has run.
The horizontal box or vertical box can be created by
indicates whether gives all children equal space allotments.
boxPackStart place objects into the
container start from the top, or left, to the bottom, or right and
boxPackEnd works along the
reverse direction. A parameter with type
Packing is required to determine how the child scales.
PackNaturalmeans the child is as big as it requests.
PackRepelwill be padded on both sides with additional space.
PackGrowincreases the size of widgets so that it covers the available space.