Outils pour utilisateurs

Outils du site


kivy_exemples_simples_pour_apprendre

Ceci est une ancienne révision du document !


Kivy: Exemples simples pour apprendre

Des exemples pour apprendre et tester.

Les fichiers sur Github

kivilabomedia sur Github

Cloner le git ou télécharger le zip.

Ouvrir un terminal dans le dossier.

python3 je_teste.py

Dans geany, ouvrir tous les fichiers et Excécuter les .py

Comprendre une application kivy avec des exemples simples

Le fichier principal doit obligatoirement s’appeler main.py pour buildozer, mais si dans les exemples il y a pléthore de main.py, l’élève débutant ne comprendra rien. Donc on fait simple et clair: le fichier principal s'appelle exemple_simple.py, à lancer avec:

python3 exemple_simple.py

Mélange FloatLayout GridLayout BoxLayout

Tuto31.png Tuto31.png

melange.py

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
 
import kivy
kivy.require('1.8.0')
 
from kivy.app import App
from kivy.core.window import Window
 
# Set window size or not with your screen resolution
Window.size = (1120, 630)
 
 
class MelangeApp(App):
    '''L'application principale.'''
    def build(FloatLayout):
        '''Melange.kv commence par "GridLayout".
        FloatLayout n'a pas d'arguments.
        '''
        pass
 
if __name__ == '__main__':
    MelangeApp().run()

melange.kv

#:kivy 1.8.0
 
GridLayout:
    spacing: 10
    padding: 10
    cols: 2
    rows: 2
    Button:
        text: "bouton 1"
    Button:
        text: "bouton 2"
    Button:
        text: "bouton 3"
    BoxLayout:
        spacing: 10
        padding: 10
        Button:
            size_hint_y: 0.5
            text: "bouton 4"
        Button:
            size_hint_y: 0.4
            text: "bouton 5"
        Button:
            size_hint_y: 0.3
            text: "bouton 6"
        Button:
            text: "bouton 7"

Le Hello World modifié

Le Hello World original comporte des notions avancées, alors que cet exemple doit être vu par un codeur qui voit du code kivy pour la première fois. Quelle mauvaise pédagogie !

fig:Tuto41.png fig:Tuto42.png

Montre les liens hello.py et hello.kv pour une action et des variables.

hello.py

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
 
import kivy
kivy.require('1.8.0')
 
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty, StringProperty
from kivy.core.window import Window
 
# Set window size or not with your screen resolution
Window.size = (1120, 630)
 
 
class Hello(FloatLayout):
    '''class des actions.'''
    # Text du bouton
    info = StringProperty()
 
    # Text du label
    label_wid = ObjectProperty()
 
    def do_action(self):
        '''Si j'appuie, je fais ça.'''
 
        # Modifie le texte du bouton défini dans le build de HelloApp
        self.info = " : Non, c'est fini"
 
        # Modification du texte avec label_wid défini au début de <Hello>:
        # et utilisé pour définir le texte du Label
        self.label_wid.text = "J'ai appuyé"
 
 
class HelloApp(App):
    '''Lit Hello.kv parce que la classe s'appelle HelloApp'''
    def build(self):
        return Hello(info=" ici !")
 
 
if __name__ == "__main__":
    HelloApp().run()

hello.kv

#:kivy 1.8.0
 
<Hello>:
    label_wid: my_custom_label
 
    BoxLayout:
        orientation: "vertical"
        padding: 200
        Button:
            text: "Appuyer" + root.info
            on_press: root.do_action()
 
        Label:
            id: my_custom_label
            text: "J'attends"
kivy_exemples_simples_pour_apprendre.1579345608.txt.gz · Dernière modification : 2020/01/18 11:06 de serge