如何在Haskell GI-Gtk listBoxRow中打印标签文本?

问题描述 投票:0回答:1

我在点击listBoxRow时尝试打印标签文本。

这是我的要点:https://gist.github.com/bigos/6cf02ff27231cfc0352394da00023f67的链接

相关代码如下所示:

  _ <- onListBoxRowSelected listbox2 (\(Just r) -> do
                                        -- rn <- listBoxRowGetIndex r
                                        cc <- containerGetChildren r
                                        dd <- widgetGetName (head cc)
                                        -- how do I print label text?
                                        -- I get the error:
                                        -- Required ancestor ‘GI.Gtk.Objects.Label.Label’ not found for type GI.Gtk.Objects.Widget.Widget’.
                                        ee <- labelGetText (head cc)
                                        putStrLn ("Clicked " ++ (show ee)))

错误

•未找到类型为“GI.Gtk.Objects.Widget.Widget”的祖先'GI.Gtk.Objects.Label.Label'。 •在'do'块的stmt中:ee < - labelGetText(head cc)在表达式中:do {cc < - containerGetChildren r; ee < - labelGetText(head cc); putStrLn(“Clicked”++(show ee))}在'onListBoxRowSelected'的第二个参数中,即

相关文件

https://hackage.haskell.org/package/gi-gtk-3.0.18/docs/doc-index.html

haskell
1个回答
0
投票

Dan Robertson提供了最有帮助的答案。获取标签对象后,必须将其转换为Label类型。这是缺失的环节。

工作代码

  _ <- onListBoxRowSelected listbox2 (\(Just r) -> do
                                         cc <- containerGetChildren r
                                         mlabel <- castTo Label (head cc)
                                         case mlabel of
                                           Nothing -> putStrLn "Not a label!"
                                           Just label -> (labelGetText label) >>= putStrLn . unpack)

非常感谢您的帮助。

相关文章How do I cast Widget to Label in Haskell's GI-Gtk?

© www.soinside.com 2019 - 2024. All rights reserved.