Question

How do I display an image in my pwd?

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image


class MyApp(App):
  def build(self):
    return Image('b1.png')

MyApp().run()
Was it helpful?

Solution

You can check the Image documentation to see that the image source is controlled by the source property. Therefore you should be able to change just one line to make it work:

    return Image(source='b1.png')

OTHER TIPS

The acceptable result for me is too comprehensive and simple.

I have a better way of doing it using .kv file:

  • Set your photo at a particular box_layout using "ids"
  • You can change your photo dynamically

Kivy.kv (file)

<main_display>:
    BoxLayout:
        orientation: "vertical"
        
        Image:
              id: imageView
              source: '<random_name>.jpg'
              allow_stretch: True
 ....

Kivy.py (file)

class main_display(BoxLayout):
    def __init__(self, **kwargs):
       super(main_display,self).__init__()
       # Photo can be reference by running the photo function once:
       Clock.schedule_once(self.photo)
    
     def photo(self,dt):
       # Replace the given image source value:
       self.ids.imageView.source = 'kivy_test.jpg'
  1. Basically, set your image file name under source attribute under "Image" in kivy.kv (file).
  2. Remember to set a clock schedule once as above and you can make your modification of your photo. You can even add some photo looping (dynamically) by changing the clock scheduler to Clock.schedule_interval(self.photo, 0.06).

I have try to directly assign the attribute 'source' from kivy.py (file) but failed with assertion error.

ENJOY and please do comment if you're not clear !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top