【问题标题】:How do I print the label text in Haskell GI-Gtk listBoxRow?如何在 Haskell GI-Gtk listBoxRow 中打印标签文本?
【发布时间】:2018-06-04 17:04:58
【问题描述】:

当我单击 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'。 • 在“do”块的语句中:ee

相关文档

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

【问题讨论】:

    标签: haskell


    【解决方案1】:

    Dan Robertson 提供了最有帮助的答案。获得标签对象后,您必须将其转换为标签类型。那是缺少的链接。

    工作代码

      _ <- 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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2020-04-13
      相关资源
      最近更新 更多