我试图用两张图片制作一个 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 中的两个 kivy 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!