1

我正在使用 wxHaskell 的教程,并希望在网格中显示表格电影的内容。这是我的代码:

{--------------------------------------------------------------------------------
   Test Grid.
--------------------------------------------------------------------------------}

module Main where

import Graphics.UI.WX
import Graphics.UI.WXCore hiding (Event)

import Database.HDBC.Sqlite3 (connectSqlite3)
import Database.HDBC


main  
  = start gui

gui :: IO ()
gui 
  = do f <- frame [text := "Grid test", visible := False] 

       -- grids
       g <- gridCtrl f []
       gridSetGridLineColour g (colorSystem Color3DFace)
       gridSetCellHighlightColour g black
       appendColumns g (movies) -- Here is error: 

--        Couldn't match expected type `[String]'
--       against inferred type `IO [[String]]'


       --appendRows g (map show [1..length (tail movies)])
       --mapM_ (setRow g) (zip [0..] (tail movies))
       gridAutoSize g

       -- layout
       set f [layout := column 5 [fill (dynamic (widget g))]
             ]       
       focusOn g
       set f [visible := True]  -- reduce flicker at startup.
       return ()
    where

    movies = do 
        conn <- connectSqlite3 "Spop.db"
        r <- quickQuery' conn "SELECT id, title, year, description from Movie where id = 1" []
        let myResult = map convRow r
        return myResult

setRow g (row,values)
  = mapM_ (\(col,value) -> gridSetCellValue g row col value) (zip [0..] values)



{--------------------------------------------------------------------------------
   Library?f
--------------------------------------------------------------------------------}

gridCtrl :: Window a -> [Prop (Grid ())] -> IO (Grid ())
gridCtrl parent props
  = feed2 props 0 $
    initialWindow $ \id rect -> \props flags ->
    do g <- gridCreate parent id rect flags
       gridCreateGrid g 0 0 0
       set g props
       return g

appendColumns :: Grid a -> [String] -> IO ()
appendColumns g []
  = return ()
appendColumns g labels
  = do n <- gridGetNumberCols g
       gridAppendCols g (length labels) True
       mapM_ (\(i,label) -> gridSetColLabelValue g i label) (zip [n..] labels)

appendRows :: Grid a -> [String] -> IO ()
appendRows g []
  = return ()
appendRows g labels
  = do n <- gridGetNumberRows g
       gridAppendRows g (length labels) True
       mapM_ (\(i,label) -> gridSetRowLabelValue g i label) (zip [n..] labels)

convRow :: [SqlValue] -> [String]
convRow [sqlId, sqlTitle, sqlYear, sqlDescription] = [intid, title, year, description]
              where intid = (fromSql sqlId)::String
                    title = (fromSql sqlTitle)::String
                    year = (fromSql sqlYear)::String
                    description = (fromSql sqlDescription)::String

我应该怎么做才能在上面的代码中出现错误(第 24 行)

4

1 回答 1

3

错误消息说appendColumns g需要一个 type 的值[String],但那movies是 type IO [[String]]

所以你需要解决两件事:

  1. movies是一个返回值的 IO 操作,但您需要它返回的值。

    代替

    appendColumns g (movies)
    

    movieList <- movies
    appendColumns g movieList
    

    (顺便说一句,第一行的括号?他们什么都不做。)

  2. 您需要提供appendColumns g一个字符串列表,但您试图给它一个字符串列表列表。您要么需要将列表列表转换为字符串列表,要么需要依次给每个字符串列表appendColumns g

    我猜你想要后者,所以你会在屏幕上为数据库中的每一行显示一行。(我对 wxhaskell 不熟悉,所以我可能会误解appendColumns它的作用。)

    movieList <- movies
    mapM_ (appendColumns g) movieList
    
于 2010-05-12T08:52:08.990 回答