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

发布于 2024-11-27 16:26:34 字数 1207 浏览 1 评论 0原文

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

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

这是一些示例代码:

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

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.

Here is some example code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦行七里 2024-12-04 16:26:34

您可以询问顶级窗口想要多大,然后将其设置为那么大:

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

要使用它,请将其粘贴到按钮的点击处理程序中:

onClicked button (containerRemove box e2 >> refresh window)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文