BoxLayout中的两个奇异图像 [英] Two kivy Images in an BoxLayout

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

问题描述

我试图制作一个带有两个图像的kivy程序,当我单击它们时这些图像应该会改变. 但是,当我尝试将两个包含图像的窗口小部件添加到BoxLayout时,我在位置0、0处获得了一个图像 为什么BoxLayout无法处理我的图像? 这是我的代码:

I tried to make an kivy program wit two images , which are supposed to change when I click on them. But when I tred to add two Widgets with the containing Images to an BoxLayout, I got one image at the position 0, 0 Why dosen't the BoxLayout work with my images? Here is my code:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class Feld(Widget):
    droga = StringProperty('hinterg.png')
    kr = StringProperty('kreuz.png')
    ks = StringProperty('kreis.png')
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.droga = self.kr

root = Builder.load_string('''
BoxLayout:
    orientation: 'horizontal'
    Feld:
        id: a1
    Feld:
        id: a2

<Feld>:
    Image:
        source: root.droga



''')

class app(App):
    def build(self):
        Window.clearcolor = (0, 0.54, 1, 1)
        return root

if __name__ == "__main__":
    app().run()

推荐答案

您的Feld实例由BoxLayout定位,但是图像显示在Feld的子级的Image小部件中.由于Feld只是一个普通的Widget,因此不会在其子代上施加任何自动位置或大小,因此两个实际图像的默认大小均为(100,100),位置的默认值为(0,0).

Your Feld instances are positioned by the BoxLayout, but the image is displayed in an Image widget that is a child of the Feld. Since Feld is just a normal Widget, it doesn't impose any automatic position or size on its children, so both actual images have the default size of (100, 100) and pos of (0, 0).

最好的解决方案是使Feld成为某个布局的子类,该子类将使其子元素在默认情况下自动填充,例如FloatLayout.

The best solution is to make Feld a subclass of some layout that will make its child fill itself by default, such as a FloatLayout.

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

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