gtk2hs:删除小部件后请求重新计算窗口大小
我有一个带有三个条目小部件和一个按钮的窗口。我使用该按钮以编程方式删除其中一个小部件。问题是主窗口在被删除后不会改变其大小以适应新的布局。
我可以想象我需要向主循环发送一些信号或事件,这会导致重新计算,但我一直无法找到这样的功能。
这是一些示例代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以询问顶级窗口想要多大,然后将其设置为那么大:
要使用它,请将其粘贴到按钮的点击处理程序中:
You can ask your top-level window how big it wants to be, and make it be that big:
To use this, stick it in the button's click-handler: