gtk2hs:删除窗口小部件后,请求重新计算窗口大小 [英] gtk2hs: Request recalculation of windows size after removing a widget

查看:69
本文介绍了gtk2hs:删除窗口小部件后,请求重新计算窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Window with three Entry widgets and one Button. I use the button to remove one of the widgets programmatically. The problem is that the main window doesn't change it's size to fit the new layout after it's been removed.

我可以想象我需要向主循环发送一些信号或事件,这会导致重新计算,但是我一直找不到这种功能.

I can imagine that I need to send some Signal or Event to the main loop which would cause the recalculation but I've been unable to find such functionality.

这是一些示例代码:

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

推荐答案

您可以询问您的顶级窗口想要多大,并使其变得如此大:

You can ask your top-level window how big it wants to be, and make it be that big:

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

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

To use this, stick it in the button's click-handler:

onClicked button (containerRemove box e2 >> refresh window)

这篇关于gtk2hs:删除窗口小部件后,请求重新计算窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆