gtk2hs:删除窗口小部件后请求重新计算窗口大小

时间:2011-08-02 17:08:29

标签: haskell gtk2hs

我有一个带有三个Entry小部件和一个Button的窗口。我使用该按钮以编程方式删除其中一个小部件。问题是主窗口在删除后不会改变它的大小以适应新的布局。

我可以想象我需要向主循环发送一些信号或事件,这会导致重新计算,但我一直无法找到这样的功能。

以下是一些示例代码:

import Graphics.UI.Gtk
import Data.IORef
import qualified Graphics.UI.Gtk as G hiding (Point)
import qualified Graphics.UI.Gtk.Gdk.EventM as E
import qualified Graphics.UI.Gtk.Abstract.Widget as W
import qualified Graphics.Rendering.Cairo as C


makeEntry :: String -> IO Entry
makeEntry str = do e <- entryNew
                   entrySetText e str
                   return e

main :: IO ()
main = do
  initGUI
  window <- windowNew
  box <- vBoxNew False 0
  G.on window G.keyPressEvent $ E.tryEvent $ do
    "Escape" <- E.eventKeyName
    C.liftIO $ G.widgetDestroy window

  set window [ containerChild := box ]

  e1 <- makeEntry "e1"
  boxPackStart box e1 PackNatural 0

  e2 <- makeEntry "e2"
  boxPackStart box e2 PackNatural 0

  e3 <- makeEntry "e3"
  boxPackStart box e3 PackNatural 0

  button <- buttonNew
  set button [ buttonLabel := "Remove" ]
  boxPackStart box button PackNatural 0

  onClicked button (containerRemove box e2)
  onDestroy window mainQuit
  widgetShowAll window
  mainGUI

1 个答案:

答案 0 :(得分:3)

你可以问你的顶级窗口它想要多大,并让它变得那么大:

refresh window = do
    Requisition w h <- widgetSizeRequest window
    windowResize window w h

要使用此功能,请将其粘贴在按钮的点击处理程序中:

onClicked button (containerRemove box e2 >> refresh window)