How to resolve the algorithm Simple windowed application step by step in the Haskell programming language
How to resolve the algorithm Simple windowed application step by step in the Haskell programming language
Table of Contents
Problem Statement
Create a window that has:
Upon clicking the button with the mouse, the label should change and show the number of times the button has been clicked.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Simple windowed application step by step in the Haskell programming language
This code is a simple GUI application written using the Haskell programming language and the Gtk library, which provides bindings to the GTK+ library for creating graphical user interfaces.
The main
function is the entry point for the application, it initializes the GTK+ GUI system and creates a window object. The window is then configured with a title and a border width of 10 pixels.
A horizontal box is created and added as the child of the window using the set
function. A label and a button are created and added as children of the horizontal box.
The label is initialized with the text "There have been no clicks yet". The button is created with the label "Click me".
An IORef is created to store the number of clicks that have been made on the button. The initial value of the IORef is 0.
The onClicked
function is used to handle clicks on the button. When the button is clicked, the current value of the IORef is read and incremented. The new value is then written back to the IORef and the label text is updated to reflect the number of clicks that have been made.
The widgetShowAll
function is used to make all of the widgets in the application visible.
The mainGUI
function is used to start the GTK+ event loop, which allows the application to respond to user input and other events.
Overall, this code demonstrates how to create a simple GUI application in Haskell using the Gtk library. The application has a window with a label and a button. When the button is clicked, the label text is updated to reflect the number of clicks that have been made.
Source code in the haskell programming language
import Graphics.UI.Gtk
import Data.IORef
main :: IO ()
main = do
initGUI
window <- windowNew
window `onDestroy` mainQuit
windowSetTitle window "Simple Windowed App"
set window [ containerBorderWidth := 10 ]
hbox <- hBoxNew True 5
set window [ containerChild := hbox ]
lab <- labelNew (Just "There have been no clicks yet")
button <- buttonNewWithLabel "Click me"
set hbox [ containerChild := lab ]
set hbox [ containerChild := button ]
m <- newIORef 0
onClicked button $ do
v <- readIORef m
writeIORef m (v+1)
set lab [ labelText := "There have been " ++ show (v+1) ++ " clicks" ]
widgetShowAll window
mainGUI
You may also check:How to resolve the algorithm Read a configuration file step by step in the Peloton programming language
You may also check:How to resolve the algorithm Power set step by step in the V programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Crystal programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the Fortran programming language
You may also check:How to resolve the algorithm String concatenation step by step in the BASIC programming language