← Volver al listado de tecnologías

Kivy Garden

Por: SiempreListo
kivygardenwidgetsextensiones

¿Qué es Kivy Garden?

Colección de widgets contribuidos por la comunidad. Extienden las capacidades de Kivy con componentes especializados.

Recursos

RecursoURL
Sitio Webhttps://kivy-garden.github.io/
GitHubhttps://github.com/kivy-garden

Instalación de Widgets

Formato Nuevo (Recomendado)

pip install kivy_garden.mapview
pip install kivy_garden.graph
pip install kivy_garden.zbarcam

Desde GitHub

pip install https://github.com/kivy-garden/mapview/archive/master.zip

MapView - Mapas Interactivos

Instalación

pip install kivy_garden.mapview

Uso Básico

from kivy_garden.mapview import MapView

class MiApp(App):
    def build(self):
        return MapView(
            lat=40.4168,      # Latitud (Madrid)
            lon=-3.7038,      # Longitud
            zoom=12
        )

Con Marcadores

from kivy_garden.mapview import MapView, MapMarker

class MapaApp(App):
    def build(self):
        mapa = MapView(lat=40.4168, lon=-3.7038, zoom=12)

        # Agregar marcador
        marcador = MapMarker(lat=40.4168, lon=-3.7038)
        mapa.add_marker(marcador)

        return mapa

En KV

MapView:
    lat: 40.4168
    lon: -3.7038
    zoom: 12

    MapMarker:
        lat: 40.4168
        lon: -3.7038

Graph - Gráficos

Instalación

pip install kivy_garden.graph

Gráfico de Líneas

from kivy_garden.graph import Graph, MeshLinePlot
from math import sin

class GraficoApp(App):
    def build(self):
        graph = Graph(
            xlabel='X',
            ylabel='Y',
            x_ticks_minor=5,
            x_ticks_major=25,
            y_ticks_major=1,
            y_grid_label=True,
            x_grid_label=True,
            padding=5,
            x_grid=True,
            y_grid=True,
            xmin=0,
            xmax=100,
            ymin=-1,
            ymax=1
        )

        plot = MeshLinePlot(color=[1, 0, 0, 1])
        plot.points = [(x, sin(x / 10.)) for x in range(100)]
        graph.add_plot(plot)

        return graph

Múltiples Series

from kivy_garden.graph import Graph, MeshLinePlot, SmoothLinePlot
from math import sin, cos

graph = Graph(xmin=0, xmax=100, ymin=-1, ymax=1)

# Serie 1
plot1 = MeshLinePlot(color=[1, 0, 0, 1])
plot1.points = [(x, sin(x / 10.)) for x in range(100)]

# Serie 2
plot2 = SmoothLinePlot(color=[0, 1, 0, 1])
plot2.points = [(x, cos(x / 10.)) for x in range(100)]

graph.add_plot(plot1)
graph.add_plot(plot2)

ZBarCam - Lector QR/Barras

Instalación

pip install kivy_garden.zbarcam
pip install pyzbar pillow

Uso

from kivy_garden.zbarcam import ZBarCam

class LectorApp(App):
    def build(self):
        return ZBarCam()

Con Callback

ZBarCam:
    id: zbarcam
    on_symbols: app.procesar_codigo(self.symbols)
class LectorApp(App):
    def procesar_codigo(self, symbols):
        for symbol in symbols:
            print(f'Tipo: {symbol.type}')
            print(f'Datos: {symbol.data.decode()}')

XCamera - Cámara Optimizada

Instalación

pip install kivy_garden.xcamera

Uso

XCamera:
    id: xcamera
    play: True

Button:
    text: 'Capturar'
    on_press: xcamera.take_picture('foto.png')

Matplotlib Backend

Instalación

pip install kivy_garden.matplotlib

Uso

import matplotlib
matplotlib.use('module://kivy_garden.matplotlib.backend_kivy')

from matplotlib import pyplot as plt
from kivy_garden.matplotlib import FigureCanvasKivyAgg

class GraficoApp(App):
    def build(self):
        fig, ax = plt.subplots()
        ax.plot([1, 2, 3, 4], [1, 4, 2, 3])

        return FigureCanvasKivyAgg(fig)

QRCode - Generador QR

Instalación

pip install kivy_garden.qrcode

Uso

QRCodeWidget:
    data: 'https://kivy.org'
from kivy_garden.qrcode import QRCodeWidget

qr = QRCodeWidget(data='Mi texto para QR')

Draggable - Widgets Arrastrables

Instalación

pip install kivy_garden.draggable

Uso

from kivy_garden.draggable import KXDraggableBehavior
from kivy.uix.label import Label

class DraggableLabel(KXDraggableBehavior, Label):
    pass
<DraggableLabel>:
    drag_cls: 'mi_grupo'
    size_hint: None, None
    size: 100, 50

Frosted Glass - Efecto Cristal

Instalación

pip install kivy_garden.frostedglass

Uso

FrostedGlass:
    size_hint: 0.8, 0.3
    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    blur_size: 20
    saturation: 1.0
    luminosity: 0.9

    Label:
        text: 'Texto sobre cristal'

Collider - Detección de Colisiones

Instalación

pip install kivy_garden.collider

Uso

from kivy_garden.collider import Collide2DPoly

# Definir polígono
poligono = [(0, 0), (100, 0), (100, 100), (0, 100)]
collider = Collide2DPoly(poligono)

# Verificar punto
if collider.collide_point(50, 50):
    print('Punto dentro del polígono')

Tabla de Widgets Populares

WidgetFunciónInstalación
mapviewMapaspip install kivy_garden.mapview
graphGráficospip install kivy_garden.graph
zbarcamLector QRpip install kivy_garden.zbarcam
xcameraCámarapip install kivy_garden.xcamera
matplotlibBackend pltpip install kivy_garden.matplotlib
qrcodeGenerar QRpip install kivy_garden.qrcode
draggableDrag & droppip install kivy_garden.draggable
frostedglassEfecto blurpip install kivy_garden.frostedglass

Testing de Widgets Garden

# test_garden.py
import unittest

class TestGardenImports(unittest.TestCase):
    def test_mapview_importable(self):
        try:
            from kivy_garden.mapview import MapView
            self.assertTrue(callable(MapView))
        except ImportError:
            self.skipTest('mapview no instalado')

    def test_graph_importable(self):
        try:
            from kivy_garden.graph import Graph
            self.assertTrue(callable(Graph))
        except ImportError:
            self.skipTest('graph no instalado')

    def test_graph_plot(self):
        try:
            from kivy_garden.graph import Graph, MeshLinePlot
            graph = Graph()
            plot = MeshLinePlot(color=[1, 0, 0, 1])
            plot.points = [(0, 0), (1, 1), (2, 4)]
            graph.add_plot(plot)
            self.assertIn(plot, graph.plots)
        except ImportError:
            self.skipTest('graph no instalado')

if __name__ == '__main__':
    unittest.main()

Probar en Dispositivo con ADB

# Verificar permisos de cámara (para ZBarCam)
adb shell dumpsys package com.ejemplo.miapp | grep camera

# Verificar permisos de ubicación (para MapView)
adb shell dumpsys package com.ejemplo.miapp | grep location

# Otorgar permisos manualmente
adb shell pm grant com.ejemplo.miapp android.permission.CAMERA
adb shell pm grant com.ejemplo.miapp android.permission.ACCESS_FINE_LOCATION

# Ver uso de red (para mapas)
adb shell dumpsys netstats | grep miapp

Resumen