steffen
steffen

Reputation: 8958

kivy: How to place images in BoxLayout?

I looked at this question, but that was something different.

I am just starting with kivy, so I guess it is something really simple :\

I want to place two images into a BoxLayout. Here is my code so far:

import kivy
kivy.require('1.9.1')

from kivy.app import App
from kivy.core.image import Image as CoreImage
from kivy.uix.boxlayout import BoxLayout

class TestApp(App):

    def build(self):
        layout = BoxLayout(orientation='horizontal')

        im1 = CoreImage("image1.png")
        im2 = CoreImage("image2.png")

        layout.add_widget(im1) # this is line 33
        layout.add_widget(im2)

        return layout

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

The program does not show a window, but prints this error message to the terminal:

 Traceback (most recent call last):
   File "./myprog.py", line 39, in <module>
     TestApp().run()
   File "/usr/lib/python3.6/site-packages/kivy/app.py", line 802, in run
     root = self.build()
   File "./myprog.py", line 33, in build
     layout.add_widget(im1)
   File "/usr/lib/python3.6/site-packages/kivy/uix/boxlayout.py", line 212, in add_widget
     pos_hint=self._trigger_layout)
   File "kivy/_event.pyx", line 438, in kivy._event.EventDispatcher.bind (kivy/_event.c:6473)
 KeyError: 'pos_hint'

The images exist. If I add pos_hint={'x':0,'y':0} to the CoreImage "constructor", I get the same error.

I am using python 3.6.0 and kivy 1.9.1

Upvotes: 0

Views: 3321

Answers (1)

el3ien
el3ien

Reputation: 5405

Core image is not a widget. You should use kivy.uix.image instead.
From kivy.core.image:

Core classes for loading images and converting them to a Texture. The raw image data can be keep in memory for further access.

So to make your example work:

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout

class TestApp(App):

    def build(self):
        layout = BoxLayout(orientation='horizontal')

        im1 = Image(source="image1.png")
        im2 = Image(source="image2.png")

        layout.add_widget(im1)
        layout.add_widget(im2)

        return layout

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

Upvotes: 1

Related Questions