在 python 文件中嵌套 kivy 小部件
Nesting kivy widgets in python file
我正在尝试在我的 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
您需要add_widget
查看https://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))
...
我正在尝试在我的 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
您需要add_widget
查看https://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))
...