Reputation: 311
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()
Upvotes: 0
Views: 1211
Reputation: 29460
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).
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.
Upvotes: 2