Загрузка изобращения из сети

text/x-python load_image.py — 1.3 KB

Содержимое файла

Using the asynchronous data loader

How to do it…
We need only a Python file and the URL in this recipe. To complete the recipe:
1. Import the usual kivy package.
2. Import the Image and Loader packages.
3. Import the Widget package.
4. Define the e2App class.
5. Define the _image_Loaded() method, which loads the image in the app.
6. Define the build() method.
7. In this method, load the image in a proxy image.
8. Define the image variable instanced as Image().
9. Return the image variable to display the load image:
import kivy
from kivy.app import App
from kivy.uix.image import Image
from kivy.loader import Loader

class e2App(App):
	#We bind the event on_load to the variable proxyImage:
	def _image_loaded(self, proxyImage):
		if proxyImage.image.texture:
			self.image.texture = proxyImage.image.texture
	def build(self):
		#loads the image
		proxyImage = Loader.image(
		#We assign the image to the proxyImage variable because we are not 
		#sure if the image exists or could be retrieved from the Web. 
		#We have the following line:
		self.image = Image()
		return self.image
if __name__ == '__main__':