Outils pour utilisateurs

Outils du site


kivy_comment_faire_un_tas_de_chose

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
kivy_comment_faire_un_tas_de_chose [2020/01/15 07:50] – [Comment lister tous les widgets dans le python] sergekivy_comment_faire_un_tas_de_chose [2022/11/19 09:49] (Version actuelle) – [Sur internet] serge
Ligne 1: Ligne 1:
-======Kivy: Comment faire un tas de chose======+======Kivy: Comment faire ou ne pas faire un tas de chose ======
 <WRAP center round box 60% centeralign> <WRAP center round box 60% centeralign>
 **{{tagpage>kivy|Toutes les pages Kivy}}**   **{{tagpage>kivy|Toutes les pages Kivy}}**  
 **[[http://translate.google.com/translate?hl=&sl=auto&tl=en&u=https%3A%2F%2Fressources.labomedia.org%2Fkivy_comment_faire_un_tas_de_chose|English Version]]** **[[http://translate.google.com/translate?hl=&sl=auto&tl=en&u=https%3A%2F%2Fressources.labomedia.org%2Fkivy_comment_faire_un_tas_de_chose|English Version]]**
 </WRAP> </WRAP>
-===== Pourquoi kivy est-il traître ? =====+<WRAP center round box 40% centeralign> 
 +**[[les_pages_kivy_en_details|Les pages Kivy en détails]]** 
 +</WRAP> 
 +===== Comment chercher ? ===== 
 +====Sur internet==== 
 +  * Dans la **[[https://kivy.org/doc/stable/|doc officielle]]**, mais les chances de trouver sont quasi nulles. 
 +  * Dans un moteur de recherche: ça finit souvent par des posts sur stackoverflow, avec des réponses <del>médiocres</del> fausses. 
 +  * **[[https://www.geeksforgeeks.org/kivy-tutorial/|geeksforgeeks.org]] est récent et plutôt bien.** 
 +====Une bonne solution: Les exemples des sources de kivy==== 
 +  * Télécharger les sources sur [[https://github.com/kivy/kivy|github]] 
 +  * Dans votre EDI créer un projet avec uniquement les [[https://github.com/kivy/kivy/tree/master/examples|exemples]] 
 +  * Rechercher dans les fichiers: si vous chercher "source:", vous aurez la liste de tous les fichiers qui utilisent "source:". Si c'est un kv, trouver le .py corespondant pour l'exécuter.
  
-**Kivy masque des erreurs python**+====Gallery of Examples==== 
 +**[[https://kivy.org/doc/stable/examples/index.html|Gallery of Examples]]** Il faut fouiller! 
 +=====Des explications avec les exemples des sources===== 
 +Une liste de scripts qui explique bien:
  
-Par exemple:+  * **pos_hint** [[https://github.com/kivy/kivy/blob/master/examples/widgets/boxlayout_poshint.py|boxlayout_poshint.py]] 
 +  * **bubble** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/bubble_test.py|bubble_test.py]] 
 +  * **camera** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/camera.py|camera.py]] 
 +  * **carousel** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/carousel_buttons.py|carousel_buttons.py]] 
 +  * **mipmap** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/label_mipmap.py|label_mipmap.py]] 
 +  * **text_size = textwrap** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/label_text_size.py|label_text_size.py]] 
 +  * **text with markup** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/label_with_markup.py|label_with_markup.py]] 
 +  * **popup** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/popup_with_kv.py|popup_with_kv.py]] 
 +  * **scatter** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/scatter.py|scatter.py]] et [[https://github.com/kivy/kivy/blob/master/examples/widgets/scatter.kv|scatter.kv]] 
 +  * **scrollview** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py|scrollview.py]] et [[https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.kv|scrollview.kv]] 
 +  * **splitter** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/splitter.py|splitter.py]] 
 +  * **tabbed panel = onglet** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/tabbedpanel.py|tabbedpanel.py]] 
 +  * **text align** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/textalign.py|textalign.py]] [[https://github.com/kivy/kivy/blob/master/examples/widgets/textalign.kv|textalign.kv]] 
 +  * **video player** : [[https://github.com/kivy/kivy/blob/master/examples/widgets/videoplayer.py|videoplayer.py]] 
 +  * **ErrorPopup(Popup)** : [[https://github.com/kivy/plyer/blob/master/examples/text2speech/main.py|main.py]] et [[https://github.com/kivy/plyer/blob/master/examples/text2speech/text2speechdemo.kv|text2speechdemo.kv]] 
 +  * **Slider** : [[https://github.com/kivy/kivy/blob/master/examples/canvas/circle.py|circle.py]] 
 +  * **Clock** : [[https://github.com/kivy/kivy/blob/master/examples/tutorials/pong/main.py|Pong]] ou [[https://github.com/kivy/kivy/blob/master/examples/canvas/mesh_manipulation.py|mesh_manipulation.py]] 
 + 
 + 
 + 
 + 
 +=====Adapter la résolution dpi sur son PC===== 
 +  * Sur votre PC, définir la taille de votre fenêtre 
 +<code python> 
 +from plyer import utils 
 + 
 +from oscpy.client import OSCClient 
 +from oscpy.server import OSCThreadServer 
 + 
 +print("Platform =", utils.platform) 
 +ANDROID = utils.platform._platform_android  # retourne True ou False 
 +print("Android =", ANDROID) 
 +if not ANDROID: 
 +    from kivy.core.window import Window 
 +    # Simulation de l'écran de mon tél: 1280*720 
 +    k = 0.8 
 +    WS = (int(720*k), int(1280*k)) 
 +    Window.size = WS 
 +    os.environ['JAVA_HOME'] = '/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64' 
 + 
 +from jnius import autoclass 
 +</code> 
 + 
 +Lancer le main.py avec: 
 +  KIVY_METRICS_FONTSCALE=1.2 python3 main.py 
 +avec une valeur "1.2" à adapter pour que vous ayez le même graphisme sur votre PC que sur votre téléphone.
  
-  * un argument qui manque dans un appel de méthode lève une erreur pour du python habituel, mais pas dans kivy. D'où un débogage rusé nécessaire ! 
  
 =====Comment lister tous les widgets dans le python===== =====Comment lister tous les widgets dans le python=====
   * **[[https://kivy.org/doc/stable/api-kivy.uix.widget.html#kivy.uix.widget.Widget.walk|walking this tree]]**   * **[[https://kivy.org/doc/stable/api-kivy.uix.widget.html#kivy.uix.widget.Widget.walk|walking this tree]]**
-Par exemple, avec toto = self: +Par exemple, extrait de [[https://github.com/sergeLabo/smartcitizen/blob/master/smartcitizen/main.py#L83|smartcitizen main.py#L83]]: 
-  print([type(widget) for widget in toto.walk(loopback=True)]) +  # Recherche de tous les widgets de self 
- +  print([type(widget) for widget in self.walk(loopback=True)]) 
-=====Comment passer d'une class à une autre=====+  # Recherche de tous les widgets de Screen1 
 +  print(self.ids.sm) 
 +  f = self.ids.sm.get_screen("first") 
 +  print([type(widget) for widget in f.walk(loopback=True)]) 
 +   
 +Sortie de [type(widget) for widget in self.walk(loopback=True)] dans SmartCitizen() 
 +<code> 
 +<class '__main__.SmartCitizen'>, 
 +<class 'kivy.uix.actionbar.ActionBar'>, 
 +<class 'kivy.uix.actionbar.ActionView'>, 
 +<class 'kivy.uix.actionbar.ActionButton'>, 
 +<class 'kivy.uix.image.Image'>, 
 +<class 'kivy.uix.screenmanager.ScreenManager'>, 
 +<class '__main__.MainScreen'>, 
 +<class 'kivy.uix.boxlayout.BoxLayout'>, 
 +<class 'kivy.uix.label.Label'>, 
 +<class 'kivy.uix.boxlayout.BoxLayout'>, 
 +...... 
 +</code> 
 +=====Comment passer un attribut d'une class à une autre=====
 En fait, **comment passer d'un objet à un autre !** En fait, **comment passer d'un objet à un autre !**
  
Ligne 24: Ligne 101:
 puis dans la liste des méthodes et attributs, ayez de nez. Si app est dans la liste: puis dans la liste des méthodes et attributs, ayez de nez. Si app est dans la liste:
   print(dir(self.app))   print(dir(self.app))
-  +  
 +====Comment récupérer self.app dans tous les objets==== 
 +Une solution élégante est utilisée dans **[[https://github.com/sergeLabo/accelerometer/blob/main/main.py|accelerometer: main.py]]** avec ces extraits modifiés pour expliquer: 
 +<code python> 
 +class AccelerometerApp(App): 
 +    def build(self): 
 +        return Accelerometer(self) 
 +         
 +class Accelerometer(BoxLayout): 
 +    def __init__(self, app, **kwargs): 
 +        super().__init__(**kwargs) 
 +        self.app = app 
 +        self.app.osc = OSC() 
 +         
 +class OSC: 
 +    def __init__(self): 
 +        self.histo = [] 
 +        self.server = OSCThreadServer() 
 +        self.server.bind(b'/histo', self.on_histo) 
 +         
 +    def on_histo(self, *args): 
 +        self.histo = .... 
 +         
 +class Screen2: 
 +    def __init__(self): 
 +        self.app = App.get_running_app() 
 +        # et appel de l'attribut histo de OSC avec 
 +        self.app.osc.histo 
 +        Clock.schedule_once(self._once, 1)
  
 +    def _once(self, dt):
 +        Clock.schedule_interval(self.update, 0.1)
 +        
 +    def update(self, dt): 
 +        blabla = self.app.osc.histo 
 +        # blabla retrouve la valeur de histo de OSC !!!!
 +</code>
  
-===== La commande magique ===== 
- 
-Dans Blender, le raccourcis magique c'est: 
-**Ctrl + A** Apply the object's transformation to its data 
- 
-Dans le *.kv, c'est: [[https://kivy.org/docs/api-kivy.lang.html|Graphical Instructions]] 
- 
-<code python> 
-    canvas: 
-        Clear 
-</code> 
  
  
Ligne 109: Ligne 210:
     ...     ...
 </code> </code>
 +
 +=====Button background_color=====
 +<code python>
 +Button:
 +    background_normal: ''
 +    background_color: 1, .3, .4, .85
 +</code>
 +Pour avoir des couleurs flashy, mettre des valeurs supérieures à 1: exemple\\
 +background_color: (0, 1.2, 0, 1)
 +{{ :media_11:button_color.png?200 |}}
 +=====Texte en gras dans un label=====
 +  * **[[https://kivy.org/doc/stable/api-kivy.core.text.markup.html|Text Markup]]**
 +<code python>
 +        Label:
 +            canvas.before:
 +                Color:
 +                    rgba: 1, 1, 1, 1
 +                Rectangle:
 +                    pos: self.pos
 +                    size: self.size
 +            size_hint_y: 0.10
 +            markup: True
 +            text: '[b]' + "Mon texte" + '[/b]'
 +            color: 1, 0, 0.5, 1
 +</code>
 +
 +
 ===== Factory ===== ===== Factory =====
 Factory peut être utilisée pour enregistrer automatiquement toute classe ou module et en instancier des classes n'importe où dans votre projet. Factory peut être utilisée pour enregistrer automatiquement toute classe ou module et en instancier des classes n'importe où dans votre projet.
  
-  * **[[http://kivy.org/docs/api-kivy.factory.html|API Factory]]** [[https://stackoverflow.com/questions/29592280/using-factory-objects-in-kivy|using-factory-objects-in-kivy]] î'm not sure of everything the Factory is used for ...Dans les exemples, c'est utilisé pour enregistrer des widgets, dans des trucs sans kv.+  * **[[http://kivy.org/docs/api-kivy.factory.html|API Factory]]** [[https://stackoverflow.com/questions/29592280/using-factory-objects-in-kivy|using-factory-objects-in-kivy]] I'm not sure of everything the Factory is used for ... Les exemples de la doc qui l'utilisent sont irréels ...
 ===== Réception sur Android en Multicast ===== ===== Réception sur Android en Multicast =====
  
Ligne 120: Ligne 248:
 android.permissions = INTERNET,CHANGE_WIFI_MULTICAST_STATE,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE android.permissions = INTERNET,CHANGE_WIFI_MULTICAST_STATE,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE
 </code> </code>
 +Les permissions définies dans buildozer.spec crée le bouton enable/disable dans Android / Paramètres / Application pour certaine permission: par défaut, il est disable. Quand ça ne marche pas, c'est la première chose à regarder.
 ===== Twisted dans kivy ===== ===== Twisted dans kivy =====
  
   * **[[https://kivy.org/doc/stable/guide/other-frameworks.html|Integrating with other Frameworks]]** propose l'intégration des 2 !   * **[[https://kivy.org/doc/stable/guide/other-frameworks.html|Integrating with other Frameworks]]** propose l'intégration des 2 !
   * **Exemple** avec [[apprendre_kivy|Apprendre Kivy]]   * **Exemple** avec [[apprendre_kivy|Apprendre Kivy]]
 +
 +=====Comment éviter ou résoudre des bugs à la compilation avec buildozer=====
 +===Comment commenter dans buildozer.spec ?===
 +Ne pas faire:
 +<code python>
 +requirements = python3,kivy,plyer,numpy,oscpy#,jnius
 +ni
 +requirements = python3,kivy,plyer,numpy,oscpy #,jnius
 +</code>
 +mais
 +<code python>
 +requirements = python3,kivy,plyer,numpy,oscpy
 +#,jnius
 +</code>
 +Le buildozer.spec est seulement presque du python. Ce commentaire maltapropo m'a donné des messages d'erreurs avec buildozer parlant de No module named 'encodings'
 +
 +=====Bugs=====
 +====Novembre 2020====
 +Voir [[https://duckduckgo.com/?q=kivy+%27xclip%27%3A+xclip++%27xsel%27%3A+xsel&t=h_&ia=web|xclip and xsel]]
 +  [CRITICAL] [Cutbuffer   ] Unable to find any valuable Cutbuffer provider. xclip - FileNotFoundError: [Errno 2] Aucun fichier ou dossier de ce type: 'xclip'
 +
 +résolu avec
 +  sudo apt install xclip
 +  
 +=====Comment avoir une action maintenue si maintien d'un Button=====
 +<code python accelerometer.kv>
 +            Button:
 +                id: recul
 +                markup: True
 +                background_color: (0, 1.2, 0, 1)
 +                font_size: "18sp"
 +                text: '[b]<<<[/b]'
 +                on_press: root.do_back_forward(-1)
 +                on_release: root.do_end()
 +            Button:
 +                id: avance
 +                markup: True
 +                background_color: (0, 1.2, 0, 1)
 +                font_size: "18sp"
 +                text: '[b]>>>[/b]'
 +                on_press: root.do_back_forward(1)
 +                on_release: root.do_end()
 +</code>
 +
 +<code python main.py>
 +    def do_back_forward(self, sens):
 +        self.bf = 1
 +        bt = Thread(target=self.back_forward_loop, args=(sens, ), daemon=True)
 +        bt.start()
 +
 +    def back_forward_loop(self, sens):
 +        while self.bf:
 +            sleep(0.1)
 +            self.gap = self.gap + sens*10
 +            if self.gap > 0: self.gap = 0
 +            l = len(self.app.osc.histo_xyz)
 +            if self.gap < -l + 500: self.gap = -l + 500
 +            print("Gap:", self.gap)
 +
 +    def do_end(self):
 +        self.bf = 0
 +</code>
  
  
-{{tag>kivy sb}}+{{tag> kivy python sb }}
kivy_comment_faire_un_tas_de_chose.1579074618.txt.gz · Dernière modification : 2020/01/15 07:50 de serge