How can I remove widgets from WxHaskell panel

2019-08-01 03:45发布

问题:

My google-fu has failed me. How can I remove widgets that I've added to a Panel () ? For example, in the following, I want the controls-panel to become empty again.

buildGUI = do
  f <- frame [ text := "Hello" ]

  controls <- panel f []
  ctext <- staticText controls [ text := "Foo" ]
  set controls [ layout := margin 5 (widget ctext) ]

  set f [ layout := widget controls ]
  {- delete ctext ? How? -}
  return ()

(I'm trying to build a dynamic GUI, and I need to get rid of the old controls when it updates).

回答1:

You could make it not visible and remove it from the layout. This doesn't actually remove it but does change the UI dynamically:

import Graphics.UI.WX

buildGUI = do
  f <- frame [ text := "Hello" ]

  controls <- panel f []
  ctext <- staticText controls [ text := "Foo" ]
  butn <- button controls [text := "Remove the Foo"]        -- I've added a button to remove Foo
  set controls [ layout := row 0 [margin 5 (widget ctext),
                                  margin 5 (widget butn) ]]

  set f [ layout := widget controls ]

  set butn [on command := do
      set ctext [visible := False]                          -- so ctext doesn't show
      set controls [layout := margin 5 (widget butn) ]]     -- so ctext doesn't take up space
  return ()

main = start buildGUI