【问题标题】:Why are buttons not rendering in a table in gtk2hs为什么按钮不在 gtk2hs 中的表格中呈现
【发布时间】:2018-03-08 20:04:48
【问题描述】:

我正在尝试在 gtk2hs 中编写一个简单的 UI。它以两个文本框和一个按钮开始。当按下按钮时,它会生成一个大小为 (m,n) 的按钮表,其中 m 和 n 取自文本框。出于某种原因,当按下按钮时,为表格分配了空间,但没有显示任何按钮!

import Graphics.UI.Gtk
import Control.Concurrent

t2l :: Int -> Int -> Int -> Int -> Int 
t2l r c rr cc = (r * cc) + c 

buildTable :: Int -> Int -> IO(Table, [Button])
buildTable r c = do
   t <- tableNew r c True
   buttons <- sequence $ take (r * c) (repeat buttonNew)
   mapM (`set` [buttonLabel := "HELLO"]) buttons
   return [tableAttachDefaults t (buttons !! (t2l rr cc r c)) cc (cc+1) rr (rr+1) | cc <- [0..(c+1)] , rr <- [0..(r+1)]]
   return (t,buttons)

main = do
   initGUI
   window <- windowNew
   mainSplit <- vBoxNew False 10
   contPannel <- hBoxNew False 5

   rowTF <- entryNew
   colTF <- entryNew
   buildBTN <- buttonNew
   set buildBTN [buttonLabel := "Build Table"]

   set window [containerChild := mainSplit]
   boxPackStart mainSplit contPannel PackGrow 0
   boxPackStart contPannel rowTF PackGrow 0
   boxPackStart contPannel colTF PackGrow 0
   boxPackStart contPannel buildBTN PackNatural 0

   on window objectDestroy mainQuit
   widgetShowAll window

   on buildBTN buttonActivated $ do
      rT <- get rowTF entryText
      cT <- get colTF entryText
      r <- return $ read rT
      c <- return $ read cT
      (t,b) <- buildTable r c 
      boxPackStart mainSplit t PackGrow 0
      widgetShowAll t
      return ()

   mainGUI

【问题讨论】:

    标签: haskell gtk2hs


    【解决方案1】:

    我不是 100% 确定为什么会出现错误,我有一些新代码可以工作:

    import Graphics.UI.Gtk
    import Control.Concurrent
    
    mkBtn :: String -> IO Button
    mkBtn label = buttonNew >>= (\b -> set b [buttonLabel := label] >> return b)
    
    buildTable :: Int -> Int -> IO(Grid)
    buildTable r c = do
       t <- gridNew
       gridSetRowHomogeneous t True
       mapM (\f -> mkBtn "Hello" >>= (\b -> gridAttach t b (f `mod` c) (f `div` c) 1 1)) [0..(r*c)-1]
       return (t) 
    
    main = do
       initGUI
       window <- windowNew
       mainSplit <- vBoxNew False 10
       contPannel <- hBoxNew False 5
    
       rowTF <- entryNew
       colTF <- entryNew
       buildBTN <- buttonNew
       set buildBTN [buttonLabel := "Build Table"]
    
       set window [containerChild := mainSplit]
       boxPackStart mainSplit contPannel PackGrow 0
       boxPackStart contPannel rowTF PackGrow 0
       boxPackStart contPannel colTF PackGrow 0
       boxPackStart contPannel buildBTN PackNatural 0
    
       on window objectDestroy mainQuit
       widgetShowAll window
    
       on buildBTN buttonActivated $ do
          rT <- get rowTF entryText
          cT <- get colTF entryText
          r <- return $ read rT
          c <- return $ read cT
          t <- buildTable r c 
          boxPackStart mainSplit t PackGrow 0
          widgetShowAll t
          return ()
    
       mainGUI
    

    也许有人会知道为什么这有效而最后一个无效?我想这就是我创建新按钮的方式。

    我改变的第一件事是从 gtk2 到 3,这使我能够使用网格而不是表格。我没有使用重复,而是使用了一个辅助函数mkBtn。其他变化就是我如何填充网格。我没有使用相当愚蠢的列表理解,而是使用mapM 并将按钮列表中的索引转换为表格坐标,而不是将表格坐标转换为列表索引(最初在t2l 中完成)

    【讨论】:

    • 如果您隔离并确定您对程序所做的更改,然后将该信息编辑到您的上述问题中,这将对其他读者更有用(更不用说更容易回答)。跨度>
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2021-09-05
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多