Kivy image not showing - python

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖

Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

Related

Kivy not loading video

I'm using the following code using kivy, trying to load a video. I created a new file that didn't have all the extra things I was doing, as to eliminate all the possible complications. I'm new to stack overflow, so if I've done something wrong don't judge...The video is in the same file, and I've tried everything I found online about these kind of errors, I've also installed pillow and ffpyplayer. The error I receive is [ERROR ] [Image ] Error loading <ironman.mp4>
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.video import Video
from kivy.uix.videoplayer import VideoPlayer
class MyApp(App):
def build(self):
video = Video(source='ironman.mp4')
video.state = "play"
video.options = {'eos': 'loop'}
video.allow_stretch = True
video.loaded = True
return video
if __name__ == "__main__":
MyApp().run()
I had the same problem but with Image. Install pillow 8.4.0.

Kivy - I can't see the picture

I am developing a mobile application. There should be a picture in the introduction of my application. Even though I put the picture and show the way, the picture does not appear when I run the application. It is replaced by a white screen. How can I solve it?
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.animation import Animation, AnimationTransition
class loginImage(Image):
pass
class loginScreen(App):
def build(self):
img = Image()
return img
loginScreen().run()
My .kv code in here:
<-loginScreen>:
source: 'images.jpeg'
kv rules are for widgets, not the App class, so your source isn't applied to anything.
Set the source of the Image widget that you return.

How can I reload a image in kivy python

this is my script ...
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(source='go.jpg')
MyApp().run()
I won't to reload it becose the image is changing and I won't to see the new won all time
you can use reload() the read the image from the disk again
this will reload even if the original data of the image is updated or changed
self.ids.image1.source = './Images/file.png'
self.ids.image1.reload()
Make your own widget class, and make the image an attribute, so you can reference it. Then use clock to schedule an interval method, to constantly reload the image.
In the example below, the update_pic method is executed once every second.
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.uix.widget import Widget
class MyImageWidget(Widget):
def __init__(self,**kwargs):
super(MyImageWidget,self).__init__(**kwargs)
self.image = Image(source='go.jpg')
self.add_widget(self.image)
Clock.schedule_interval(self.update_pic,1)
def update_pic(self,dt):
self.image.reload()
class MyApp(App):
def build(self):
return MyImageWidget()
MyApp().run()
You can use the Image.reload method
def build(self):
img = Image(source='go.jpg')
Clock.schedule_interval(lambda dt: img.reload(), 0.2) #5 per second
return img

Setting kivy window size not working

I follwed this stackoverflow question, but neither alternatives worked.
This is my code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
class MyApp(App):
def build(self):
return Label(text='text')
if __name__ == '__main__':
Window.size = (1366, 768)
MyApp().run()
Sometimes the size works, Kivy creates a screen with size 800x600 then changes it to 1366x768. And sometimes Kivy creates a screen with size 800x600 then changes it to 1366x768, but then back to 800x600.
And if I change my code to:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '200')
class MyApp(App):
def build(self):
return Label(text='text')
if __name__ == '__main__':
MyApp().run()
With this code, nothing happens on my screen. I'm using Kivy v1.9.2-dev0. What I should do to fix it?
Put the Config settings before all the other imports - it's too late after importing Window, as the config has already been accessed to determine its initial size and your new settings are ignored.
Make your window non-resizable
from kivy.config import Config
Config.set('graphics','resizable',0)
set the window size
from kivy.core.window import Window
Window.size = (600, 500)
see example at https://kivyapps.wordpress.com/video-streaming-using-kivy-and-python/

Creating an Image Popup in Kivy

I'm trying to write a very basic Kivy program that will have an image popup if the right user input is given. I had one image I'm using to test with, but I keep getting the error
"pygame.error: couldn't load cattemp.jpg"
The image is in the same directory as the file, so I know that's not the issue.
Here's what the code looks like:
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.core.image import Image
def test():
popup = Popup(title='Test popup',
content=Image('cattemp.jpg').texture,
size_hint=(None, None), size=(400, 400))
Any help would be hugely appreciated.
Thank you!
You can only load PNG.
Use PIL to convert the file or use another.
Create your image with something like:
import PIL
im = PIL.Image.open('cattemp.jpg')
im.save('cattemp.png')
Then you use the image in your program
content=Image('cattemp.png').texture,
...
Can't load doesn't necessarily mean it's not found.

Categories