简体   繁体   English

在 python 文件中嵌套 kivy 小部件

[英]Nesting kivy widgets in python file

I'm trying to recreate the below MDCard, I wrote in kivy language, in my python file.我正在尝试重新创建下面的 MDCard,我在 python 文件中用 kivy 语言编写。 Although I can do it fine in kivy language I'm struggling to do it in Python.虽然我可以在 kivy 语言中很好地做到这一点,但我在 Python 中努力做到这一点。

kv file: .kv 文件:

    MDCard:
        size_hint: 1, None
        elevation: 6
        oritentation: "horizontal"
        spacing: 10
        Image:
            source: "test_image.jpeg"
            allow_stretch: True
            keep_ratio: False
            size_hint_x: 0.5
        BoxLayout:
            orientation: "vertical"
            BoxLayout:
                MDLabel:
                    text: "Test"
                MDLabel:
                    text: "Test text"
            MDLabel:
                text: "Longer text here to test wrap around of MDLabel"

py file: .py 文件:

    md_card = MDCard(size_hint=(1,None), elevation=6, spacing=10,
        Image(source="test_image.jpeg", allow_stretch=True, keep_ratio=False, size_hint_x=0.5),
        BoxLayout(orientation="vertical", BoxLayout(MDLabel(text="Test"), MDLabel(text="Test text")), 
            MDLabel(text="Longer text here to test wrap around of MDLabel")))

At the moment, with the py file I get a SyntaxError:目前,使用 py 文件我得到一个 SyntaxError:

Image(source="test_image.jpeg", allow_stretch=True, keep_ratio=False, size_hint_x=0.5),
^
SyntaxError: positional argument follows keyword argument

You need to add_widget see https://kivy.org/doc/stable/guide/widgets.html你需要add_widgethttps://kivy.org/doc/stable/guide/widgets.html

md_card = MDCard(size_hint=(1,None), elevation=6, spacing=10)
md_card.add_widget(Image(source="test_image.jpeg", allow_stretch=True, keep_ratio=False, size_hint_x=0.5))
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM