如何判断鼠标按钮是否在 gtk2hs 中类似标签的小部件(或容器?)上按下?

发布于 2024-10-19 11:37:06 字数 748 浏览 9 评论 0原文

下面是一个使用 gtk2hs 的简单示例,它添加了一个标签,然后在其上添加了一个单击处理程序。当您单击标签时,永远不会调用buttonPressEvent 处理程序。按钮可以放在容器中,但是......容器会触发按钮按下信号吗?

我有一个矩形区域,其中有一些文本(当前使用标签),我需要知道用户是否单击了它。我不希望它看起来像一个按钮。

{-# LANGUAGE PackageImports #-}

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Gdk.EventM
import Graphics.UI.Gtk.Gdk.GC
import "mtl" Control.Monad.Trans(liftIO)

main = do
  initGUI
  window <- windowNew
  window `onDestroy` mainQuit

  label0 <- labelNew $ Just "static label"
  widgetAddEvents label0 [ButtonPressMask] -- is this necessary? Still doesn't work with it, though
  label0 `on` buttonPressEvent $ tryEvent $ do
    liftIO $ putStrLn "static label clicked"
  containerAdd window label0

  widgetShowAll window
  mainGUI

Below is a simple example using gtk2hs that adds a label and then a click handler on it. The buttonPressEvent handler is never called when you click on the label. The button could be put in a container, but... do containers fire the button pressed signal?

I have a rectangular area that has some text in it (currently using label) that I need to know if the user clicked in it. I don't want it to look like a button.

{-# LANGUAGE PackageImports #-}

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Gdk.EventM
import Graphics.UI.Gtk.Gdk.GC
import "mtl" Control.Monad.Trans(liftIO)

main = do
  initGUI
  window <- windowNew
  window `onDestroy` mainQuit

  label0 <- labelNew $ Just "static label"
  widgetAddEvents label0 [ButtonPressMask] -- is this necessary? Still doesn't work with it, though
  label0 `on` buttonPressEvent $ tryEvent $ do
    liftIO $ putStrLn "static label clicked"
  containerAdd window label0

  widgetShowAll window
  mainGUI

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-10-26 11:37:06

您想为此使用事件框。它是一个捕获事件的容器。

这是一个有效的版本。

{-# LANGUAGE PackageImports #-}

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Gdk.EventM
import Graphics.UI.Gtk.Gdk.GC
import "mtl" Control.Monad.Trans(liftIO)

main = do
  initGUI
  window <- windowNew
  window `onDestroy` mainQuit

  label0 <- labelNew $ Just "static label"

  eventBox0 <- eventBoxNew
  eventBox0 `on` buttonPressEvent $ tryEvent $ do
    liftIO $ putStrLn "static label clicked"

  containerAdd eventBox0 label0
  containerAdd window eventBox0

  widgetShowAll window
  mainGUI

You want to use an event-box for that. It's a container that captures events.

Here's a version that works.

{-# LANGUAGE PackageImports #-}

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Gdk.EventM
import Graphics.UI.Gtk.Gdk.GC
import "mtl" Control.Monad.Trans(liftIO)

main = do
  initGUI
  window <- windowNew
  window `onDestroy` mainQuit

  label0 <- labelNew $ Just "static label"

  eventBox0 <- eventBoxNew
  eventBox0 `on` buttonPressEvent $ tryEvent $ do
    liftIO $ putStrLn "static label clicked"

  containerAdd eventBox0 label0
  containerAdd window eventBox0

  widgetShowAll window
  mainGUI
放肆 2024-10-26 11:37:06

GTK 文档说没有按下按钮发出的信号标签。

正确的是,我想说,你应该使用按钮来完成这些事情。不过,不可否认的是,gtk2hs 可以采用更严格的类型并捕获这一点。

或者,您可以将链接添加到标签文本,然后覆盖适当的信号。

The GTK docs say that there's no such signal as a button press emitted by labels.

Rightly so, I'd say, you're supposed to use buttons for such things. Admittedly, though, gtk2hs could be more strictly typed and catch that.

Alternatively, you can add links to your label text and then override the appropriate signals.

心如狂蝶 2024-10-26 11:37:06

使标签可选择:

set label0 [labelSelectable := True]

Make the label selectable:

set label0 [labelSelectable := True]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文