python-3.x - 如何在 Wxpython PseudoDC 中返回 wx.Rect 中的 ID

标签 python-3.x wxpython

我正在为我在 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/

相关文章:

python - 如何为 Python 2 和 3 上传通用 Python Wheel?

python - wxPython 菜单不显示图像

python - 发布一个 wxPython 应用程序 : Give out scripts or compile in Exe, 等?

python - wxPython:计算器的用户输入显示

wxPython wx.TextCtrl 动态调整大小以填充面板宽度

python - 代码在 IDE 中有效,但在终端控制台中无效

python - 未绑定(bind)本地错误 : local variable 'y' referenced before assignment

python - 在 Jupyter 中将警告转化为错误

python - Pip 安装挂起

python - 在 Ubuntu 11.10 上安装 wxPython