Kivy: Liste des éléments graphiques Widgets de comportement et Gestion d'écrans

Gérer plusieurs écrans: options, menu … fig:Screenmanager1.png

#! /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.uix.screenmanager import ScreenManager, Screen
 
class MenuScreen(Screen):
    '''Lit <MenuScreen>: dans kv soit:
    <MenuScreen>:
        BoxLayout:
            Button:
                text: 'Définir les options'
                on_press: root.manager.current = 'Options'
            Button:
                text: 'Quitter'
    '''
    pass
 
class SettingsScreen(Screen):
    # Lit <SettingsScreen>: dans kv
    pass
 
class ScreenmanagerApp(App):
    def build(self):
        sm = ScreenManager()
        # Appel de la classe qui lit kv
        sm.add_widget(MenuScreen(name='Menu'))
        # Appel de la classe qui lit kv
        sm.add_widget(SettingsScreen(name='Options'))
        # Retourne root = sm
        return sm
 
if __name__ == '__main__':
    ScreenmanagerApp().run()

screenmanager.kv

#:kivy 1.8.0
 
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Définir les options'
            font_size: 36
            on_press: root.manager.current = 'Options'
        Button:
            font_size: 36
            text: 'Quitter'
 
<SettingsScreen>:
    BoxLayout:
        Button:
            text: "Mon bouton d'options"
            font_size: 36
        Button:
            text: 'Retour au menu'
            font_size: 36
            on_press: root.manager.current = 'Menu'

Le widget Carousel fournit la vue carrousel = mobile-friendly classique où vous pouvez glisser entre les diapositives.

Il faut toujours que la doc rajoute des notions nouvelles dans l'exemples de bases: nul !

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
 
import kivy
kivy.require('1.8.0')
 
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.factory import Factory
 
class Example1(App):
 
    def build(self):
        carousel = Carousel(direction='right')
        for i in range(4):
            src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
            image = Factory.AsyncImage(source=src, allow_stretch=True)
            carousel.add_widget(image)
        return carousel
 
Example1().run()

carousel_1.py

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
 
import kivy
kivy.require('1.8.0')
 
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.carousel import Carousel
 
class MainScreen(Screen):
    pass
 
class Screen1(Screen):
    pass
 
class Screen2(Screen):
    pass
 
class Screen3(Screen):
    pass
 
class Screen4(Screen):
    pass
 
class SettingsScreen(Screen):
    pass
 
class JsonScreen(Screen):
    pass
 
class OpenScreen(Screen):
    pass
 
SCREENS = { 0: (MainScreen,       "Menu"),
            1: (Screen1,          "Ecran 1"),
            2: (Screen2,          "Ecran 2"),
            3: (Screen3,          "Ecran 3"),
            4: (Screen4,          "Ecran 4"),
            5: (SettingsScreen,   "Options"),
            6: (JsonScreen,       "Texte libre"),
            7: (OpenScreen,       "Page libre")
           }
 
class Carousel_1App(App):
    def build(self):
        carousel = Carousel(direction='right')
        for i in range(8):
            carousel.add_widget(SCREENS[i][0](name=SCREENS[i][1]))
        return carousel
 
 
if __name__ == '__main__':
    Carousel_1App().run()

carousel_1.kv

#:kivy 1.8.0
 
<MainScreen>:
    BoxLayout:
        Label:
            text: 'Menu principal'
            font_size: 36
 
<Screen1>:
    BoxLayout:
        Label:
            text: 'ecran 1'
            font_size: 36
 
<Screen2>:
    BoxLayout:
        Label:
            text: 'ecran 2'
            font_size: 36
 
<Screen3>:
    BoxLayout:
        Label:
            text: 'ecran 3'
            font_size: 36
 
<Screen4>:
    BoxLayout:
        Label:
            text: 'ecran 4'
            font_size: 36
 
<SettingsScreen>:
    BoxLayout:
        Label:
            text: "Mon bouton d'options"
            font_size: 36
 
<JsonScreen>:
    BoxLayout:
        Label:
            text: "Mon bouton d'options"
            font_size: 36
 
<OpenScreen>:
    BoxLayout:
        Label:
            text: "Rien"
            font_size: 36
  • kivy_liste_des_elements_graphiques_widgets_de_comportement_et_gestion_d_ecrans.txt
  • Dernière modification: 2018/10/25 13:47
  • par serge