kivy-画布坐标系的问题(全局与局部)



使用我的kv文件的一部分,如下所示,我想在ShowRectangle的画布上画线。

然而,我遇到了以下问题。

小部件"ParentOfShowRectangle"将BoxLayout的85%提供给"ShowRectanger",将15%提供给某些按钮。

"ShowRectangle"的散射正确渲染到85%以上。但是,canvas.after中的线并不是在屏幕的上部85%内绘制的,而是与下部15%(按钮)相交。即便如此,它们也有正确的长度等(以85%的比例缩放)。

看起来,它只是错过了坐标系15%的适当偏移。这可能是一个错误选择的坐标系的问题吗?我想不出原因。

ShowRectangle继承自BoxLayout。ParentOfShowRectangle继承自Screen。


kv文件

<ShowRectangle>:
    canvas.after:
        Line:
            width: 3
            points: self.size[0] * 0.05, self.size[1] * 0.05, self.size[0] * 0.05, self.size[1] * 0.95, self.size[0] * 0.95, self.size[1] * 0.95, self.size[0] * 0.95, self.size[1] * 0.05
            close: True
    Scatter:
        pos: self.pos
        size: self.size
        size_hint: 1, 1
<ParentOfShowRectangle>:
    BoxLayout:
        orientation: 'vertical'
        ShowRectangle:
            size_hint: 1, 0.85
            size: 1, 0.85
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.15
            height: 1, 0.15
            Button:
                text: 'Cancel.'
                font_size: root.button_font_size
                size_hint: 1, 0.6
                height: ...
            Button:
                text: 'Ok.'
                font_size: root.button_font_size
                size_hint: 1, 0.6
                height: ...

找到了使用self.center进行相对定位的解决方案:

self.center[0]-self.size[0] * 0.45, self.center[1]-self.size[1] * 0.45, 
self.center[0]+self.size[0] * 0.45, self.center[1]-self.size[1] * 0.45, 
self.center[0]+self.size[0] * 0.45, self.center[1]+self.size[1] * 0.45, 
self.center[0]-self.size[0] * 0.45, self.center[1]+self.size[1] * 0.45

最新更新