2024-09-29 09:38:22 发布
网友
我一直在尝试用Python编写简单的CAD应用程序。我在修补pyglet,得到了一些结果:,但我决定切换回Gtk,我撞到了墙:
我无法获取EventBox上的鼠标指针位置(对于pyglet,它是图片中窗口应用程序左下角的标签)。为它设计了什么信号?或者我应该用另一种方法?你知道吗
我将感谢任何信息或资源。提前谢谢。你知道吗
在stovfl的帮助下,解决了这个问题。 应用程序 由绘图区域、框架、事件框和标签组成,用于显示鼠标指针坐标。绘图区域被添加到长方体,长方体被添加到框架,框架被添加到栅格。你知道吗
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)
motion-notify-event
self.locationLabel.set_alignment(0, 0)
Question: How to detect the mouse position over EventBox?
The Gtk.EventBox widget is a subclass of Gtk.Bin which also has its own window. It is useful since it allows you to catch events for widgets which do not have their own window.
Gtk.EventBox
Gtk.Bin
Adds the events in the bitfield events to the event mask for self.
events
self
A set of bit-flags to indicate which events a window is to receive.GDK_MOTION_NOTIFY the pointer (usually a mouse) has moved
A set of bit-flags to indicate which events a window is to receive.
Gtk.EventBox应该有标志,而不是窗口!你知道吗
box = Gtk.EventBox() box.connect("motion-notify-event", self.on_mouse_move) box.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
在stovfl的帮助下,解决了这个问题。 应用程序 由绘图区域、框架、事件框和标签组成,用于显示鼠标指针坐标。绘图区域被添加到长方体,长方体被添加到框架,框架被添加到栅格。你知道吗
解决方案是:
POINTER_MOTION_MASK
添加到事件框:box.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
。你知道吗motion-notify-event
,该方法读取并更新一个标签(在左下角;它与self.locationLabel.set_alignment(0, 0)
对齐)。你知道吗Gtk.EventBox
应该有标志,而不是窗口!你知道吗相关问题 更多 >
编程相关推荐