如何解决如何在kivy中的Gridlayout中的图像下获取文本?
在这里,我只是想在每张图片下获得几行文字。您可以在下面看到我尝试的代码,但显然不起作用。 https://carbon.nesbot.com/docs/#api-localization
rows:2
cols: 3
size_hint: 1,.7
pos_hint: {'center_y':0.55}
spacing: 100
padding: 40
Image:
source: app.strips
StackLayout:
pos: self.pos
MDLabel:
text: "testing"
Image:
source: app.cowboyRibeye
Image:
source: app.tbone
Image:
source: app.boneinStrip
Image:
source: app.ribeye
Image:
source: app.porterhouse
解决方法
您可以在布局中创建结合Image
和Label
的自定义类,并使用该自定义类代替Image,如下所示:
GridLayout:
rows:2
cols: 3
size_hint: 1,.7
pos_hint: {'center_y':0.55}
spacing: 100
padding: 40
MyImage:
image_source: app.strips
subtext: 'strips'
MyImage:
image_source: app.cowboyRibeye
subtext: 'cowboyRibeye'
MyImage:
image_source: app.tbone
subtext: 'tbone'
MyImage:
image_source: app.boneinStrip
subtext: 'boneinStrip'
MyImage:
image_source: app.ribeye
subtext: 'ribeye'
MyImage:
image_source: app.porterhouse
subtext: 'porterhouse'
<MyImage@GridLayout>:
image_source: ''
subtext: ''
cols: 1
canvas.before:
Color:
rgba: (1,1,1)
Rectangle:
size: self.size
pos: self.pos
Image:
source: root.image_source
size_hint: 0.9,0.9
Label:
text: root.subtext
halign: 'center'
color: 0,1
size_hint: 0.1,0.1
更新:
在自定义类的size_hint
和Image
中添加了Label
属性,以正确显示图像。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。