Kivy中的图像尺寸

3

我正在创建一个非常简单的练习应用程序,但在Kivy GUI方面遇到了一些麻烦。我希望所有图像都具有相同的尺寸,并且如果可能的话,创建一条分隔所有垂直盒式布局的线。

: 名称:'Prices'

BoxLayout:

    orientation:'vertical'
    canvas.before:
        Rectangle:
            source:'back_azul.png'
            pos: self.pos
            size: self.size

    BoxLayout:
        orientation:'horizontal'
        height:'30dp'
        size_hint_y:None

        Button:

            size_hint_x:0.25
            text:"Back to Menu"

            opacity: 1 if self.state == 'normal' else .5
            background_color:0,0,0,0
            on_release:app.root.current="main"
            font_size:20





    BoxLayout:
        background_color:0,10,10,1
        padding:5
        Image:
            source:"camisa.jpg"

        Label:
            text:"01 Camisa social"
            bold:True
            font_size:11
        Label:
            text:"R$: 8,00"
            font_size:15

    BoxLayout:
        padding:5
        Image:
            source:"peca.jpg"

        Label:
            text:"01 Camisa Polo"
            font_size:11
            bold:True

        Label:
            text:"R$:6,00"
            font_size:10
    BoxLayout:
        padding:5
        Image:
            source:"terno.jpg"

        Label:
            text:"01 Terno c/Calca"
            font_size:11
            bold:True
        Label:
            text:"R$: 28,00"
            font_size:10
    BoxLayout:
        padding:5
        Image:
            source:"vestido.jpg"
        Label:
            text:"01 Vestido"
            font_size:11
            bold:True
        Label:
            text:"R$: 70,00"
            font_size:10

enter image description here


您的图片似乎具有不同的比例。您想匹配所有图像的宽度或高度,还是想要“拉伸”一些图像以使宽度和大小相同? - PalimPalim
你的帖子第四行有一个名为“Prices”的名称,这是代码吗?它为什么在那里? - PalimPalim
名称:“价格”,定义我正在使用的屏幕的名称。 - Icaro Amorim
我希望这些图片的宽度保持一致。 - Icaro Amorim
1个回答

16

图片等宽处理:

选项1:您可以设置图片的宽度,但必须将相应的大小提示设置为“None”。

Image:
    size_hint_y: None
    source:"Astronaut3.jpg"
    width: 100
    allow_stretch: True

选项2:使用 size_hint

Image:
    source:"Astronaut2.jpg"
    size_hint_x: 0.4
    allow_stretch: True

创建一条线

有不同的选择。您可以使用kivy Graphics中的Line。一个简单明了的解决方案是使用Label,将其设置为您选择的颜色,然后将其缩小。

Label:
    canvas.before:
        Color: 
            rgba: (1,1,1,1)
        Rectangle:
            size: self.size
            pos: self.pos
    size_hint_y: None
    height: 1

样例应用程序

以下是一个样例应用程序中提到的所有内容。在编码时重复自己并不是好的实践,但我在这里尽可能地模仿您的问题。

在此输入图片描述

样例应用程序示例:

from kivy.app import App
from kivy.base import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout

Builder.load_string("""
<rootwi>:
    orientation:'vertical'

    BoxLayout:
        padding:5
        Image:
            source:"Astronaut2.jpg"
            size_hint_x: 0.4
            allow_stretch: True

        Label:
            text:"01 Camisa Polo"
            font_size:11
            bold:True

        Label:
            text:"R$:6,00"
            font_size:10
    Label:
        canvas.before:
            Color: 
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_y: None
        height: 1

    BoxLayout:
        padding:5
        Image:
            source:"Astronaut3.jpg"
            size_hint_x: 0.4
            allow_stretch: True

        Label:
            text:"01 Camisa Polo"
            font_size:11
            bold:True

        Label:
            text:"R$:6,00"
            font_size:10

    Label:
        canvas.before:
            Color: 
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_y: None
        height: 1

    BoxLayout:
        padding:5
        Image:
            size_hint_y: None
            source:"Astronaut2.jpg"
            width: 100
            allow_stretch: True

        Label:
            text:"01 Camisa Polo"
            font_size:11
            bold:True

        Label:
            text:"R$:6,00"
            font_size:10
    Label:
        canvas.before:
            Color: 
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_y: None
        height: 1

    BoxLayout:
        padding:5
        Image:
            size_hint_y: None
            source:"Astronaut3.jpg"
            width: 100
            allow_stretch: True

        Label:
            text:"01 Camisa Polo"
            font_size:11
            bold:True

        Label:
            text:"R$:6,00"
            font_size:10


""")
class rootwi(BoxLayout):
    pass


class MyApp(App):
    def build(self):
        return rootwi()

if __name__ == '__main__':
    MyApp().run()

非常感谢。 - Icaro Amorim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接