删除按钮后调用布局管理器

时间:2011-11-29 02:18:08

标签: layout haskell button resize gtk2

我正在使用Haskell和gtk2hs绑定在GTK中编写一个简单的计算器。 我正在尝试使用Glade在Windows计算器中实现基本/科学视图。

我有一个GTKTable按钮,但当我尝试隐藏其中一些按钮所在的空白区域时。要隐藏按钮,我有一些像这样的代码:

bSqrt <- xmlGetWidget xml castToButton "bSqrt"
widgetHide bSqrt

但是当我隐藏我想要的四个按钮时,我在右侧有一个如下的间隙: enter image description here

我是GTK的新手,我找不到像你在java swing中那样的布局管理器。有更简单的方法吗?我可以以某种方式调用布局管理器来为我调整按钮的大小吗?我无法在文档中找到这样做的方法。

提前致谢,

1 个答案:

答案 0 :(得分:1)

考虑使用HBoxVBox的嵌套组合来实现表格式效果。在“科学”按钮的VBox上调用widgetHideAll将隐藏该列并根据需要刷新显示。

import Control.Monad (forM_)
import Data.IORef as IORef
import qualified Graphics.UI.Gtk as Gtk

data Mode = Basic | Scientific

main = do
    Gtk.initGUI

    window <- Gtk.windowNew
    outerVbox <- Gtk.vBoxNew False 0

    -- Create a "table" of buttons as an HBox of VBoxes.
    hbox <- Gtk.hBoxNew True 5

    -- Load the "table" with dummy 'basic' buttons.
    forM_ [0..2] $ \i -> do
        vbox <- Gtk.vBoxNew False 5
        forM_ [0..2] $ \j -> do
            dummy <- Gtk.buttonNewWithLabel $ show (3*i+j :: Int)
            Gtk.boxPackStartDefaults vbox dummy
        Gtk.boxPackStartDefaults hbox vbox

    -- Load rightmost column with 'scientific' buttons.
    scibox <- Gtk.vBoxNew False 5
    forM_ [0..2] $ \j -> do
        dummy <- Gtk.buttonNewWithLabel $ "sci" ++ show (j :: Int)
        Gtk.boxPackStartDefaults scibox dummy
    Gtk.boxPackStartDefaults hbox scibox

    -- Begin in Scientific mode.
    let mode = Scientific
    modeRef <- IORef.newIORef mode

    -- Create a mode-toggling Button.
    button <- Gtk.buttonNewWithLabel $ getButtonText mode
    Gtk.on button Gtk.buttonActivated $
        toggleMode button modeRef scibox

    -- Pack the "table" and button vertically into window.
    Gtk.boxPackStartDefaults outerVbox hbox
    Gtk.boxPackStartDefaults outerVbox button
    Gtk.containerAdd window outerVbox

    -- Standard Gtk stuff.
    Gtk.onDestroy window Gtk.mainQuit
    Gtk.widgetShowAll window
    Gtk.mainGUI

getButtonText Basic = "Switch to Scientific"
getButtonText Scientific = "Switch to Basic"


toggleMode button modeRef scibox = do
    mode <- IORef.readIORef modeRef
    case mode of
        Basic -> do
            IORef.writeIORef modeRef Scientific
            Gtk.buttonSetLabel button $ getButtonText Scientific
            Gtk.widgetShowAll scibox
        Scientific -> do
            IORef.writeIORef modeRef Basic
            Gtk.buttonSetLabel button $ getButtonText Basic
            Gtk.widgetHideAll scibox