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

2024-09-29 09:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我无法获取EventBox上的鼠标指针位置(对于pyglet,它是图片中窗口应用程序左下角的标签)。为它设计了什么信号?或者我应该用另一种方法?你知道吗

我将感谢任何信息或资源。提前谢谢。你知道吗


Tags: 方法信息应用程序gtk信号图片标签资源
2条回答

stovfl的帮助下,解决了这个问题。 应用程序 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)

解决方案是:

  1. POINTER_MOTION_MASK添加到事件框:box.add_events(Gdk.EventMask.POINTER_MOTION_MASK)。你知道吗
  2. 用一个方法连接方框的motion-notify-event,该方法读取并更新一个标签(在左下角;它与self.locationLabel.set_alignment(0, 0)对齐)。你知道吗

Question: How to detect the mouse position over EventBox?

Gtk.EventBox应该有标志,而不是窗口!你知道吗

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

相关问题 更多 >