我正在为我在 Wxpython 中开发的程序制作一个“框选择”工具。我正在使用PseudoDC类进行绘图。
用户应该能够绘制一个框,通过 ID 在节点图上选择绘制的节点对象,但我无法找出获取框选择内的 ID 的好方法。
到目前为止,我已经得出以下结论:
def OnLeftUp(self, event):
...
# This is in the mouse event method which calls the *BoxSelectHitTest* method below.
self._selectednodes = self.BoxSelectHitTest(
wx.Point(self._bboxRect[2]/2,
self._bboxRect[3]/2)
)
...
def BoxSelectHitTest(self, pt):
# self._bboxRect is the wx.Rect of the Box Select
average = (self._bboxRect[3] + self._bboxRect[2])/2
idxs = self._pdc.FindObjects(pt[0], pt[1], int(average))
hits = [
idx
for idx in idxs
if idx in self._nodes
]
# Return the node objects from the IDs
if hits != []:
nodes = []
for Id in hits:
nodes.append(self._nodes[Id])
return nodes
else:
return []
这显然不是真正的框选。它更像是循环选择的糟糕版本。 (平均半径只是我试图使其“起作用”的尝试。)
我在 PseudoDC 中找不到可以返回给定 wx.Rect 内对象 ID 的方法。 有没有一种方法可以做到这一点,或者应该如何正确实现?
谢谢。
最佳答案
我通过查看 wx.Rect 的文档发现了这一点,所以我想我会将其发布在这里。
使用wx.Rect.Intersects
检查bboxrect是否与每个节点的矩形相交并返回它们:
def BoxSelectHitTest(self, bboxrect):
nodehits = []
for node in self._nodes.values():
if bboxrect.Intersects(node.GetRect()) == True:
nodehits.append(node)
if nodehits != []:
return nodehits
else:
# Make sure we deselect everything
for node in self._selectednodes:
node.SetSelected(False)
node.Draw(self._pdc)
self._selectednodes = []
return []
关于python-3.x - 如何在 Wxpython PseudoDC 中返回 wx.Rect 中的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59923219/