AkamaSoft
4 / 5
Mots 1801-2400 / 2895
kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition import time import random class FirstScreen(Screen): pass class SecondScreen(Screen): pass class ColourScreen(Screen): colour = ListProperty([1., 0., 0., 1.]) class MyScreenManager(ScreenManager): def new_colour_screen(self): name = str(time.time()) s = ColourScreen(name=name, colour=[random.random() for _ in range(3)] + [1]) self.add_widget(s) self.current = name root_widget = Builder.load_string(''' #:import FadeTransition kivy.uix.screenmanager.FadeTransition MyScreenManager: transition: FadeTransition() FirstScreen: SecondScreen: <FirstScreen>: name: 'first' BoxLayout: orientation: 'vertical' Label: text: 'first screen!' font_size: 30 Image: source: 'logo.png' allow_stretch: False keep_ratio: False BoxLayout: Button: text: 'goto second screen' font_size: 30 on_release: app.root.current = 'second' Button: text: 'get random colour screen' font_size: 30 on_release: app.root.new_colour_screen() <SecondScreen>: name: 'second' BoxLayout: orientation: 'vertical' Label: text: 'second screen!' font_size: 30 Image: source: 'logo1.jpg' allow_stretch: False keep_ratio: False BoxLayout: Button: text: 'goto first screen' font_size: 30 on_release: app.root.current = 'first' Button: text: 'get random colour screen' font_size: 30 on_release: app.root.new_colour_screen() <ColourScreen>: BoxLayout: orientation: 'vertical' Label: text: 'colour {:.2},{:.2},{:.2} screen'.format(*root.colour[:3]) font_size: 30 Widget: canvas: Color: rgba: root.colour Ellipse: pos: self.pos size: self.size BoxLayout: Button: text: 'goto first screen' font_size: 30 on_release: app.root.current = 'first' Button: text: 'get random colour screen' font_size: 30 on_release: app.root.new_colour_screen() ''') class ScreenManagerApp(App): def build(self): return root_widget ScreenManagerApp().run()

Quelques interactions de widgets supplémentaires

Jetons un coup d'œil à un exemple assez intéressant, où nous allons lier l'interaction de deux widgets ensemble en utilisant la méthode bind.

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.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
 
class SimpleApp(App):
    def build(self):
        b = BoxLayout(orientation="vertical")
        t = TextInput(font_size=100,text="default",size_hint_y=None, height=100)
        f = FloatLayout()
        s = Scatter()
        l = Label(text="default", font_size=150)
 
        t.bind(text=l.setter("text"))
        f.add_widget(s)
        s.add_widget(l)
        b.add_widget(t)
        b.add_widget(f)
        return b
 
 
if __name__ == "__main__":
    SimpleApp().run()

Au fur et à mesure que notre application se complexifie, il devient difficile de maintenir la construction du widget tress et la déclaration explicite des liaisons. Pour surmonter ces lacunes, le langage kv est une alternative, également connue sous le nom de langage kivy ou kvlang.

Le langage kv vous permet de créer l'arborescence des widgets de manière déclarative, il permet des prototypes très rapides et des modifications agiles de l'interface utilisateur. Cela aide également en séparant la logique de l'application de l'interface utilisateur.

Comment charger un fichier KV?

Vous pouvez suivre deux méthodes pour charger le fichier kv dans votre application.

    1. Par convention de nom - Le kivy recherche le fichier portant le même nom que votre application en commençant par la lettre minuscule moins «app» s'il est présent dans le nom de votre application.
  1. Par convention de nom - Le kivy recherche le fichier portant le même nom que votre application en commençant par la lettre minuscule moins «app» s'il est présent dans le nom de votre application.
  • Par convention de nom - Le kivy recherche le fichier portant le même nom que votre application en commençant par la lettre minuscule moins «app» s'il est présent dans le nom de votre application.
  • SimpleApp - simple.kv
    
    Builder.load_file("filename.kv")
    #or
    Builder.load_string('''
    ''') #you can directly put your kv file as string using this approach.
    

    La racine est déclarée en déclarant la classe de votre widget racine

    Widget:
    
    <Widget>:
    
    1. app: fait référence à l'instance de l'application
    2. root: il fait référence au widget de base ou au widget racine
    3. self: fait référence au widget actuel
  • app: fait référence à l'instance de l'application
  • root: il fait référence au widget de base ou au widget racine
  • self: fait référence au widget actuel
  • 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
     
     
    class ScatterTextWidget(BoxLayout):
        pass
     
     
    class SimpleApp(App):
        def build(self):
            return ScatterTextWidget()
     
     
    if __name__ == "__main__":
        SimpleApp().run()
    
    <ScatterTextWidget>: orientation:
    Page 4 sur 5 (2895 mots au total)
    80% 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