如何判断鼠标按钮是否在 gtk2hs 中类似标签的小部件(或容器?)上按下?
下面是一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想为此使用事件框。它是一个捕获事件的容器。
这是一个有效的版本。
You want to use an event-box for that. It's a container that captures events.
Here's a version that works.
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.
使标签可选择:
Make the label selectable: