AkamaSoft
5 / 5
Mots 2401-2895 / 2895
'vertical' TextInput: id: my_textinput font_size: 150 size_hint_y: None height: 200 text: 'default' FloatLayout: Scatter: Label: text: my_textinput.text font_size: 150

Langage Python et Kivy

Le langage Python et kivy permet à tout développeur d'écrire beaucoup plus facilement du code lisible pour n'importe quelle application, et cela devient également moins complexe dans la définition des propriétés et des liaisons pour divers widgets.

Essayons de mélanger le langage python et kivy dans l'exemple suivant.

from kivy.app import App
 
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
import random
 
 
class Text(BoxLayout):
    def change_label_colour(self, *args):
        colour = [random.random() for i in range(3)] + [1]
        label = self.ids['my_label']
        label.color = colour
 
 
class SimpleApp(App):
    def build(self):
        return Text()
 
 
if __name__ == "__main__":
    SimpleApp().run()
#:import color random
<Text>:
    orientation: 'vertical'
    TextInput:
        id: my_textinput
        font_size: 150
        size_hint_y: None
        height: 200
        text: 'default'
        on_text: my_label.color = [color.random() for i in range(3)] + [1]
    FloatLayout:
        Scatter:
            Label:
                id: my_label
                text: my_textinput.text
                font_size: 150

Propriétés Kivy

Les propriétés sont un moyen plus simple de définir des événements et de les lier entre eux. Il existe différents types de propriétés pour décrire le type de données que vous souhaitez gérer.

  • StringProperty
  • Propriété numérique
  • BoundedNumericProperty
  • ObjectProperty
  • DictProperty
  • ListProperty
  • OptionProperty
  • AliasProperty
  • Propriété booléenne
  • ReferenceListProperty
  • StringProperty
  • Propriété numérique
  • BoundedNumericProperty
  • ObjectProperty
  • DictProperty
  • ListProperty
  • OptionProperty
  • AliasProperty
  • Propriété booléenne
  • ReferenceListProperty
  • Nous devons déclarer les propriétés au niveau de la classe. Voici un exemple simple pour montrer comment nous pouvons utiliser les propriétés dans une application.

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.uix.button import Button
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import ListProperty
     
     
    class RootWidget(BoxLayout):
     
        def __init__(self, **kwargs):
            super(RootWidget, self).__init__(**kwargs)
            self.add_widget(Button(text='btn 1'))
            cb = CustomBtn()
            cb.bind(pressed=self.btn_pressed)
            self.add_widget(cb)
            self.add_widget(Button(text='btn 2'))
     
        def btn_pressed(self, instance, pos):
            print('pos: printed from root widget: {pos}'.format(pos=pos))
     
     
    class CustomBtn(Widget):
        pressed = ListProperty([0, 0])
     
        def on_touch_down(self, touch):
            if self.collide_point(*touch.pos):
                self.pressed = touch.pos
                # we consumed the touch. return False here to propagate
                # the touch further to the children.
                return True
            return super(CustomBtn, self).on_touch_down(touch)
     
        def on_pressed(self, instance, pos):
            print('pressed at {pos}'.format(pos=pos))
     
     
    class TestApp(App):
     
        def build(self):
            return RootWidget()
     
     
    if __name__ == '__main__':
        TestApp().run()
    

    Notre CustomBtn n'a pas de représentation visuelle et apparaît donc en noir. Vous pouvez toucher / cliquer sur la zone noire pour voir la sortie sur votre console.

    Animations

    Nous pouvons ajouter des animations dans une application kivy en utilisant l'animation ou animationTransition pour animer les propriétés du widget. Dans l'exemple, le rectangle se déplace vers un emplacement aléatoire à chaque clic sur le rectangle.

    from kivy.base import runTouchApp
    from kivy.lang import Builder
    from kivy.uix.widget import Widget
    from kivy.animation import Animation
    from kivy.core.window import Window
    from random import random
     
    Builder.load_string('''
    <Root>:
        ARect:
            pos: 500, 300
    <ARect>:
        canvas:
            Color:
                rgba: 0, 0, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
    ''')
     
    class Root(Widget):
        pass
     
    class ARect(Widget):
        def circle_pos(self):
            Animation.cancel_all(self)
            random_x = random() * (Window.width - self.width)
            random_y = random() * (Window.height - self.height)
     
            anim = Animation(x=random_x, y=random_y,
                             duration=4,
                             t='out_elastic')
            anim.start(self)
     
        def on_touch_down(self, touch):
            if self.collide_point(*touch.pos):
                self.circle_pos()
     
    runTouchApp(Root())
    
    Panneau de
    Page 5 sur 5 (2895 mots au total)
    100% lu

    Auteur: Mvondo bekey anael

    ceo | founder
    COMPETENCES
    • • Maitrise des environnements Unix, Linux, Mac, Windows, IOS et Android
    • • Programmation Orientée Objet : Python, Ruby, C++, Java, PHP, Js
    • • Certifier linux Lpi (101, 102, 202, 203, 301, 303)
    • • Certifier Adwords Professional , Certifier Ceh (6, 7, 8, 9, 10, 11)
    • • Maîtrise parfaite de : Docker, VMware sphère, Microsoft Hyper, Citrix, Virtual box,promox vm
    Annonce