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

时间:2017-12-22 01:21:51

标签: haskell

当我点击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.Label.Label'   “GI.Gtk.Objects.Widget.Widget”。 •在&#39; do&#39;块:ee&lt; -   labelGetText(head cc)在表达式中:       做{cc&lt; - containerGetChildren r;            ee&lt; - labelGetText(head cc);            putStrLn(&#34; Clicked&#34; ++(show ee))}在'onListBoxRowSelected'的第二个参数中,即

相关文件

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

1 个答案:

答案 0 :(得分: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?