Saurabh Shrivastava
Saurabh Shrivastava

Reputation: 1484

How to set image from external server to kv file

My code works perfectly for image saved in same directory.

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

Builder.load_string('''
<MenuScreen>:
    GridLayout:
        cols: 1
        Button:
            on_press: root.val1()
            Image:
                source: "myimage.PNG"
                size: self.parent.width, self.parent.height
                allow_stretch: True
                keep_ratio: False

''')

class MenuScreen(Screen):
    def val1(self):
        print "i am executed"

sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)

class MainApp(App):
    def build(self):
        return sm

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

What changes should be made in this code if i want to take image from external source ie

Image:
    source: "http://example.com/myimage.jpg"

Obviously this does not work. Please help.

Upvotes: 1

Views: 884

Answers (1)

Nykakin
Nykakin

Reputation: 8747

Try using AsyncImage instead. From the documentation:

To load an image asynchronously (for example from an external webserver), use the AsyncImage subclass:

aimg = AsyncImage(source='http://mywebsite.com/logo.png')

This can be useful as it prevents your application from waiting until the image is loaded. If you want to display large images or retrieve them from URL’s, using AsyncImage will allow these resources to be retrieved on a background thread without blocking your application.

Upvotes: 1

Related Questions