Gtk:如何检测EventBox上的鼠标位置?

问题描述 投票:1回答:2

我一直在尝试用Python编写简单的CAD应用程序。我在修补pyglet时,得到了一些结果:simple CAD app with pyglet,但我决定改回Gtk,然后撞到了墙:

我不能将鼠标指针放在EventBox上(使用pyglet,它是图片中窗口应用程序左下角的标签)。为此设计了什么信号?还是应该使用其他方法?

我将不胜感激任何信息或资源。提前致谢。

python gtk
2个回答
1
投票

问题:如何检测EventBox上的鼠标位置?

Gtk.EventBox应该有标志,而不是窗口!

box = Gtk.EventBox()
box.connect("motion-notify-event", self.on_mouse_move)
box.add_events(Gdk.EventMask.POINTER_MOTION_MASK)

0
投票

借助>解决方案得以实现。应用程序a simple CAD in python/gtk它由一个绘图区域,一个框架,一个事件框和一个标签组成,用于显示鼠标指针坐标。将绘制区域添加到框,将框添加到框架,然后将其添加到网格。from gi.repository import Gtk from gi.repository import Gdk # (...) box = Gtk.EventBox() box.add_events(Gdk.EventMask.POINTER_MOTION_MASK) # 1 box.connect("button-press-event", self.on_click) box.connect("motion-notify-event", self.on_mouse_move) # 2 self.canvas = Gtk.DrawingArea() self.canvas.connect("draw", self.on_draw) self.canvas.set_size_request(800, 600) box.add(self.canvas) grid = Gtk.Grid() frame = Gtk.Frame() frame.set_label("KHAD") frame.add(box) grid.attach(frame, 0, 1, 1, 1) self.add(grid) # (...) self.locationLabel = Gtk.Label("X,Y") self.locationLabel.set_alignment(0, 0) grid.attach(self.locationLabel, 0, 2, 1, 1)

解决方案是:

    POINTER_MOTION_MASK添加到事件框中:box.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
  1. 将框的motion-notify-event与方法连接,该方法读取并更新标签(左下角;与self.locationLabel.set_alignment(0, 0)对齐)。
© www.soinside.com 2019 - 2024. All rights reserved.