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

2024-10-04 05:28:06 发布

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

我的kv文件的一部分如下所示,我想在ShowRectangle的画布上画线。你知道吗

但是,我遇到了以下问题。你知道吗

小部件“ParentOfShowRectangle”为“ShowRectangle”提供BoxLayout的上85%,为某些按钮提供下15%。你知道吗

“ShowRectangle”的散点正确地渲染到85%的上限。但是,在画布后不是在屏幕的85%上方绘制,而是与15%下方相交(按钮)。即便如此,它们的长度还是正确的(按85%比例缩放)。你知道吗

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

ShowRectangle继承自BoxLayout。 ShowRectangle的父级从屏幕继承。你知道吗


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: ...

Tags: 文件posselfsize屏幕画布按钮font
1条回答
网友
1楼 · 发布于 2024-10-04 05:28:06

找到了使用的解决方案自我中心相对定位:

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

相关问题 更多 >