How to display an image using kiwi

How to display 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() 
+11
python kivy
source share
2 answers

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 only one line to make it work:

  return Image(source='b1.png') 
+15
source share

acceptable result for me is too comprehensive and simple. I have a better way:

  • Set your photo in a specific box_layout field using "identifiers"
  • 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' 

Basically, set an arbitrary file name in the source attribute to kivy.kv (file). I tried to directly assign the "source" attribute from kivy.py (file), but failed with an approval error.

This is normal, in the Kivy.py file, remember to set the timetable once, as described above, and you can make changes to your photo. You can even add some Clock.schedule_interval(self.photo, 0.06) photos (dynamically) by changing the clock scheduler to Clock.schedule_interval(self.photo, 0.06) .

ENJOY!

0
source share

All Articles