【问题标题】:Python - Kivy 1.7.2 - Layout over image not ligning up properlyPython - Kivy 1.7.2 - 图像上的布局没有正确排列
【发布时间】:2013-12-13 21:34:30
【问题描述】:

所以我对 Kivy 完全陌生。我挣扎了一会儿才让图像居中显示。我理解它的方式,一切都是一个小部件,任何东西都可以嵌套在它下面(大多数情况下)。如果这不是真的,请纠正我。

我现在有了我的图像,想开始布置我的 UI。不过,一切都在左下角对齐。

import kivy
kivy.require("1.7.2")

from kivy.config import Config
Config.set('graphics', 'width', '540')
Config.set('graphics', 'height', '960')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.graphics import Rectangle


class RootWidget(Widget):
  pass

class MyApp(App):

    def build(self):
        # self.root = root = RootWidget(source = "snowy_mountains.jpg")
        self.root = root = RootWidget()
        root.bind(size = self._update_rect,
                  pos = self._update_rect)
        with root.canvas.before:
          self.rect = Rectangle(size = root.size,
                                pos = root.pos)

        return root

    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size


if __name__ == "__main__":
  app = MyApp()
  app.run()

还有我的 .kv ...

#:kivy 1.7.2

<RootWidget>:
    BoxLayout:
        orientation: "vertical"
        padding: 10
        spacing: 15
        GridLayout:
            rows: 2
            size_hint: 1, .333
            Label:
                text: "App Name"
            Label:
                text: "Some Info ..."
        GridLayout:
            cols: 2
            spacing: 15
            Button:
                text: "1"
            Button:
                text: "2"
            Button:
                text: "3"
            Button:
                text: "4"

我已经通过 kivycatalog 对此进行了测试,效果很好。我将我从那里所做的完全复制到我的 .kv 中,并将其全部嵌套在 &lt;RootWidget&gt; 下(这是我的图像所在的位置)。我一直在浏览 API 和在线(在 kivy 上没有很多用户贡献......)并且无法弄清楚它为什么会这样。我什至尝试只使用普通的小部件而不是子类化 Image。呸……有什么想法吗?

【问题讨论】:

    标签: python layout kivy


    【解决方案1】:

    这是因为 Kivy 中小部件的默认大小是 100x100 像素 - 小部件不会自动占用所有可用空间。你需要设置size属性:

    <RootWidget>:
        BoxLayout:
            size: root.size # this line was missing
            orientation: "vertical"
            # ...
    

    此问题的更多信息:Kivy - base application has strange alignment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-10
      • 2013-08-10
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多