WxHaskell/Tips and tricks

From HaskellWiki
< WxHaskell
Revision as of 18:30, 5 August 2008 by EricKow (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Getting Started


  • retrieving the text of a TextCtrl: mytext <- get mytextEntry text
  • staticText misbehaves on resize: make sure that you are giving the widget enough space... for example, if you want a one-line staticText, you should
    • use hfill on the staticText
    • not use hfill (or hfloat...) or any other widgets in the same row


  • Don't bother looking at the the examples yet, the API is better for this
  • See the Haddock-generated documentation for Graphics.UI.WX.Layout
  • Got layout troubles? Packing things inside yet another panel seems to help sometimes

Scroll Bars

  • scrollbars are just windows. You create a scrollFrame, and any widgets you want inside the scrollbars, you make this scrollFrame their parent
  • see samples/wx/ImageViewer.hs
  • the following widgets (controls) already include scrollbars : listBox

Mac OS X (Darwin)

  • for ghci usage download EnableGui.hs from wxhaskell.sourceforge.net/download/EnableGUI.hs and
    ghc -fffi -package wx EnableGUI.hs
    ghci -package wx HelloTest.hs EnableGui.hs
    -- followed by :m +EnableGUI and
    enableGUI >> main

Other tips

Idle event

Use the idle event for automation.

Long computations

Scenario: you've got a looooooong computation and you want to update a progress bar or you want to have a 'STOP' button which aborts the computation


  • wxcApp(Safe)Yield

FIXME: elaborate on this!

Managing multiple windows

Scenario: you have a main window MAIN with some information. You want to create a secondary window PARAMS in which the user edits some configuration stuff. When the user closes PARAMS, the window MAIN should be updated to reflect the new settings.

You have some code which looks like

set paramsButton [ on command := do createParamsWindow
                                    updateMainWindow ]

Problem: MAIN is not updated... at least, not until you call PARAMS a second time.


  • This is NOT a problem with Haskell laziness

Explanation: updateMainWindow tries to read the new configuration value, but it does not succeed, because no configuration values have changed. Why not? Simpler than it looks: all the createParamsWindow function does is creates a window with some widgets, assign some commands to said widgets, and return. There's nothing in the createParamsWindow code that says the function should only return when PARAMS has been closed.

Solution: Pass createParamsWindow a function which updates the MAIN window:

set paramsButton [ on command := do createParamsWindow updateMainWindow ]

When the user closes PARAMS, one of the final things it should do is call this update function