使用haskell-gi GTK信号

时间:2017-06-25 00:21:03

标签: haskell gtk gtk2hs

Haskell Gi-GTK noob在这里。和GTK noob一般。

我有一张图片,我已将其包装在Eventbox中。现在,我想检测用户何时按下事件框(mousedown)。一些谷歌搜索指向我使用button-press-event。我的代码如下。

drag <- imageNewFromFile "rszh.png"
dragevents <- eventBoxNew
containerAdd dragevents drag
set dragevents [widgetHalign := AlignEnd, widgetValign := AlignEnd]
onWidgetButtonPressEvent dragevents (print "Hello world")

GHC无法使用以下隐藏错误消息编译:

panedraggin.hs:30:42: error:
    • Couldn't match type ‘IO ()’
                     with ‘GI.Gdk.Structs.EventButton.EventButton -> IO Bool’
      Expected type: GI.Gtk.Objects.Widget.WidgetButtonPressEventCallback
        Actual type: IO ()
    • Possible cause: ‘print’ is applied to too many arguments
      In the second argument of ‘onWidgetButtonPressEvent’, namely
        ‘(print "Hello world")’
      In a stmt of a 'do' block:
        onWidgetButtonPressEvent dragevents (print "Hello world")
      In the expression:
        do { Gtk.init Nothing;
             window <- windowNew WindowTypeToplevel;
             onWidgetDestroy window mainQuit;
             windowMaximize window;
             .... }

我做错了什么?

1 个答案:

答案 0 :(得分:4)

错误消息已经说明了:它需要EventButton -> IO Bool类型的函数,print "Hello world"类型为IO ()

然而,您可以轻松地将其转换为:

onWidgetButtonPressEvent dragevents (const $ print "Hello world" >> return True)

因此,使用const $我们暂时忽略EventButton参数(稍后您可以决定将事件参数考虑在内),并使用>> return True,我们确保之后打印时,我们返回True(意味着回调成功)。