Kivy es una herramienta GUI independiente de la plataforma en Python. Como se puede ejecutar en Android, IOS, Linux y Windows, etc. B谩sicamente se usa para desarrollar la aplicaci贸n de Android, pero eso no significa que no se pueda usar en aplicaciones de escritorio.
Widget de imagen:
El widget Imagen se utiliza para mostrar una imagen. Para usar el widget de imagen, debe importar:
from kivy.uix.image import Image, AsyncImage
porque el m贸dulo kivy.uix.image
tiene toda la funcionalidad relacionada con las im谩genes.
Las im谩genes se pueden cargar en la aplicaci贸n a trav茅s de dos tipos:
1) Del sistema:
wimg = Image(source='mylogo.png')
2) Carga as铆ncrona:
para cargar una imagen de forma as铆ncrona (por ejemplo, desde un servidor web externo), use la subclase AsyncImage:aimg = AsyncImage(source='http://mywebsite.com/logo.png')
Nota: De manera predeterminada, la imagen est谩 centrada y cabe dentro del cuadro delimitador del widget. Si no quiere eso, puede establecer allow_stretch en True y keep_ratio en False.
Basic Approach to create multiple layout in one file: 1) import kivy 2) import kivyApp 3) import image 4) set minimum version(optional) 5) create App class 6) return Image/layout/widget 7) Run an instance of the class
A continuaci贸n se muestra el c贸digo de c贸mo puede usar las im谩genes en su c贸digo:
C贸digo #1:
Imagen simple del sistema (debe estar en la carpeta en la que se guarda el archivo .py)
# Program to explain how to add image in kivy 聽聽 # import kivy module聽聽聽 import kivy聽 聽聽聽聽 # base Class of your App inherits from the App class.聽聽聽 # app:always refers to the instance of your application聽聽 from kivy.app import App 聽聽 # this restrict the kivy version i.e聽 # below this kivy version you cannot聽 # use the app or software聽 kivy.require('1.9.0') 聽聽 # The Image widget is used to display an image # this module contain all features of images from kivy.uix.image import Image 聽聽 # creating the App class class MyApp(App): 聽聽 聽聽聽聽# defining build() 聽聽聽聽聽聽 聽聽聽聽def build(self): 聽聽聽聽聽聽聽聽聽聽 聽聽聽聽聽聽聽聽# return image 聽聽聽聽聽聽聽聽return Image(source ='download.jpg') 聽聽 # run the App MyApp().run()
Salida:
聽
C贸digo #2:
驴C贸mo podemos agregar AsyncImage, es decir, desde el servidor web (externo)?
# Simple program to show how we add AsyncImage in kivy App 聽聽 # import kivy module聽聽聽 import kivy聽 聽聽聽聽 # base Class of your App inherits from the App class.聽聽聽 # app:always refers to the instance of your application聽聽 from kivy.app import App 聽聽 # this restrict the kivy version i.e聽 # below this kivy version you cannot聽 # use the app or software聽 kivy.require('1.9.0') 聽聽 # The Image widget is used to display an image # this module contains all features of images from kivy.uix.image import AsyncImage 聽聽 聽聽 # creating the App class class MyApp(App): 聽聽 聽聽聽聽# defining build() 聽聽聽聽聽聽 聽聽聽聽def build(self): 聽聽聽聽聽聽聽聽聽聽 聽聽聽聽聽聽聽聽# return image 聽聽聽聽聽聽聽聽return AsyncImage(source ='http://kivy.org/logos/kivy-logo-black-64.png') 聽聽 # run the App MyApp().run()
Producci贸n:
Ahora se me ocurri贸 una cosa: 驴c贸mo puede cambiar el tama帽o, la posici贸n, etc. de la imagen? El siguiente c贸digo tambi茅n lo explicar谩:
C贸digo #3:
# Program to Show how to use images in kivy 聽聽 # import kivy module聽聽聽 import kivy聽 聽聽聽聽 # base Class of your App inherits from the App class.聽聽聽 # app:always refers to the instance of your application聽聽 from kivy.app import App 聽聽 # this restrict the kivy version i.e聽 # below this kivy version you cannot聽 # use the app or software聽 kivy.require('1.9.0') 聽聽 # The Image widget is used to display an image # this module contain all features of images from kivy.uix.image import Image 聽聽 # The Widget class is the base class required for creating Widgets from kivy.uix.widget import Widget 聽聽 # to change the kivy default settings we use this module config from kivy.config import Config 聽聽聽 # 0 being off 1 being on as in true / false # you can use 0 or 1 && True or False Config.set('graphics', 'resizable', True) 聽聽聽 聽聽 # creating the App class class MyApp(App): 聽聽 聽聽聽聽# defining build() 聽聽聽聽聽聽 聽聽聽聽def build(self): 聽聽聽聽聽聽聽聽聽聽 聽聽聽聽聽聽聽聽# loading image 聽聽聽聽聽聽聽聽self.img = Image(source ='download.jpg') 聽聽 聽聽聽聽聽聽聽聽# By default, the image is centered and fits 聽聽聽聽聽聽聽聽# inside the widget bounding box. 聽聽聽聽聽聽聽聽# If you don鈥檛 want that, 聽聽聽聽聽聽聽聽# you can set allow_stretch to 聽聽聽聽聽聽聽聽# True and keep_ratio to False. 聽聽聽聽聽聽聽聽self.img.allow_stretch = True 聽聽聽聽聽聽聽聽self.img.keep_ratio = False 聽聽 聽聽聽聽聽聽聽聽# Providing Size to the image 聽聽聽聽聽聽聽聽# it varies from 0 to 1 聽聽聽聽聽聽聽聽self.img.size_hint_x = 1 聽聽聽聽聽聽聽聽self.img.size_hint_y = 1 聽聽 聽聽聽聽聽聽聽聽# Position set 聽聽聽聽聽聽聽聽self.img.pos = (200, 100) 聽聽 聽聽聽聽聽聽聽聽# Opacity adjust the fadeness of the image if 聽聽聽聽聽聽聽聽# 0 then it is complete black 聽聽聽聽聽聽聽聽# 1 then original 聽聽聽聽聽聽聽聽# it varies from 0 to 1 聽聽聽聽聽聽聽聽self.img.opacity = 1 聽聽聽聽聽聽聽聽聽聽 聽聽 聽聽聽聽聽聽聽聽# adding image to widget 聽聽聽聽聽聽聽聽s = Widget() 聽聽聽聽聽聽聽聽s.add_widget(self.img) 聽聽 聽聽聽聽聽聽聽聽# return widget 聽聽聽聽聽聽聽聽return s 聽聽 # run the app MyApp().run()
Producci贸n:
Publicaci贸n traducida autom谩ticamente
Art铆culo escrito por YashKhandelwal8 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA