【问题标题】:Nesting kivy widgets in python file在 python 文件中嵌套 kivy 小部件
【发布时间】:2020-05-31 16:59:08
【问题描述】:

我正在尝试在我的 python 文件中重新创建下面的 MDCard,我用 kivy 语言编写。虽然我可以在 kivy 语言中很好地做到这一点,但我正在努力用 Python 做到这一点。

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 文件:

    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")))

目前,使用 py 文件我得到一个 SyntaxError:

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

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    你需要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))
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多