【问题标题】:Kivy-Image only centered some of the time at run timeKivy-Image 仅在运行时的某些时间居中
【发布时间】:2017-06-21 15:41:39
【问题描述】:

我有一个带有按钮的 GridLayout 和一个要在按钮内显示的图像。当我运行我的应用程序时,按钮中的图像有时会居中,有时不会。为什么?我认为它应该始终居中。

.kv 文件:

MyLayout:
    cols: 2                
    height: self.minimum_height
    pos: root.pos

    Button:
        size_hint_y: None
        height: self.parent.width/2
        Image:
            source: 'Images/employee/userprofile.png'
            size: self.parent.size
            center_x: self.parent.center_x
            center_y: self.parent.center_y

下图是我每次都期待的样子:

这是我有时会得到的:

【问题讨论】:

  • 您要调整图像大小吗?是否应该展开并填充整个按钮?
  • 我添加了我的意思的屏幕截图。基本上在运行时,当我应该(我认为)获得第一个结果时,我会得到其中任何一个结果。

标签: image python-3.x kivy grid-layout kivy-language


【解决方案1】:

这是center_xrighttop 等别名属性的常见问题。在 kv 中设置它们不会自动绑定到小部件的大小(将其添加到 kivy 并不是那么简单听起来),所以虽然图像最初定位正确,但如果之后调整大小,则位置不会更新,因为父母的位置没有改变,只有孩子的大小!幸运的是,在这种情况下,根据特定运行中调度的时间安排,似乎有一半时间可以工作的代码并不少见。

无论如何,解决方案很简单,在绑定中显式引用孩子的大小,因此每次更改时都会重新计算表达式。

        center_x: self.width and self.parent.center_x
        center_y: self.height and self.parent.center_y

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2018-01-07
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多