Kivy 上的图像大小 [英] Image Size on Kivy

查看:23
本文介绍了Kivy 上的图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为实践创建一个非常简单的应用程序,但我在使用 Kivy GUI 时遇到了一些问题.我想让所有图像都具有相同的尺寸,如果可能的话,可以创建一个分隔所有垂直框布局的线条.

Im creating a very simple aplication for pratice, and I´m having some trouble with Kivy GUI. I would like to get all the images on the same size,and if it´s possible creat aline that separetes all the vertical box layouts.

:名称:'价格'

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

推荐答案

相同宽度的图片:

选项1:可以设置宽度,但必须将对应的尺寸提示设置为无.

Same width of the images:

Option 1: you can set the width, but must set the corresponding size hint to None.

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

选项 2:使用 size_hint

Option2: use the size_hint

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

创建一条线

同样有不同的选择.您可以使用 kivy Graphics 中的 Line.一个简单而直接的解决方案是使用标签并使其成为您选择的颜色,然后使其非常小.

Creating a line

Again there are different options. You could use the Line from kivy Graphics. An easy and straightforward solution is to use a Label and make it your color of choice and then make it really small.

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

<小时>

示例应用

以下是一个示例应用中提到的所有内容.在编码时重复自己不是一个好习惯,但我在这里这样做是为了尽可能地反映您的问题.


Sample App

Here are all the things mentioned in one sample App. It is not good practice to repeat yourself when coding, but I am doing it here to mirror your question as close as possible.

示例应用示例:

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

这篇关于Kivy 上的图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆