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

Prochaine révision
Révision précédente
kivy_comment_faire_un_tas_de_chose [2018/03/22 14:58] – créée 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> 
 +**{{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]]** 
 +</WRAP> 
 +<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.
  
-===== Pourquoi kivy est-il traître ? =====+====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:
  
-**Kivy masque des erreurs python**+  * **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]]
  
-Par exemple: 
  
-  * 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 ! 
  
-===== La commande magique ===== 
  
-Dans Blenderle raccourcis magique c'est:+=====Adapter la résolution dpi sur son PC===== 
 +  * Sur votre PCdéfinir la taille de votre fenêtre 
 +<code python> 
 +from plyer import utils
  
-[[https://wiki.blender.org/index.php/User:Fade/Doc:2.6/Manual/3D_interaction/Transform_Control/Reset_Object_Transformations#Apply_Object_transformations|**''%%Ctrl%%'' ''%%+%%'' ''%%A%%''**]]+from oscpy.client import OSCClient 
 +from oscpy.server import OSCThreadServer
  
-Dans le *.kvc'est:+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él1280*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.
 +
 +
 +=====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]]**
 +Par exemple, extrait de [[https://github.com/sergeLabo/smartcitizen/blob/master/smartcitizen/main.py#L83|smartcitizen main.py#L83]]:
 +  # Recherche de tous les widgets de self
 +  print([type(widget) for widget in self.walk(loopback=True)])
 +  # 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 !**
 +
 +Faire des print(de quoi ?). Commencer par
 +  print(dir(self))
 +puis dans la liste des méthodes et attributs, ayez de nez. Si app est dans la liste:
 +  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> <code python>
-    canvas+class AccelerometerApp(App): 
-        Clear+    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> </code>
-[[https://kivy.org/docs/api-kivy.lang.html|§ Graphical Instructions]] 
  
-===== Pourquoi définir les attributs comme attributs de classe ? =====+ 
 + 
 +===== Pourquoi définir les attributs appelés dans *.kv comme attributs de classe ? =====
  
 **Parce que c'est comme ça ? Quand lama fâché, lui toujours faire ainsi !** **Parce que c'est comme ça ? Quand lama fâché, lui toujours faire ainsi !**
  
 +Voir l'exemple ci-dessous.
 ===== Comment changer la couleur d'un Rectangle ? ===== ===== Comment changer la couleur d'un Rectangle ? =====
  
Ligne 91: Ligne 210:
     ...     ...
 </code> </code>
-===== Factory ===== 
  
-The factory can be used to automatically register any class or module and instantiate classes from it anywhere in your project.+=====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>
  
-  * http://kivy.org/docs/api-kivy.factory.html 
  
 +===== Factory =====
 +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]] 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 104: 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 =====
  
-Les API ont un+  * **[[https://kivy.org/doc/stable/guide/other-frameworks.html|Integrating with other Frameworks]]** propose l'intégration des 
 +  * **Exemple** avec [[apprendre_kivy|Apprendre Kivy]]
  
-''%%tourne_pour_toujours()%%''+=====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'
  
-pour les lancer.+=====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'
  
-Les deux ne peuvent pas tourner dans un thread.+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>
  
-La solution est: [[https://github.com/kivy/kivy/tree/master/examples/frameworks/twisted|twisted dans kivy]] mais qui ne marche qu'en python2Twisted n'est pas encore porté entièrement en python3 en janvier 2018.+<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()
  
-Erreur obtenue+    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>
  
-''%%_threadselect interface of twisted is not available on Python 3%%'' 
  
-{{tag>kivy}}+{{tag> kivy python sb }}
kivy_comment_faire_un_tas_de_chose.1521730685.txt.gz · Dernière modification : 2018/03/22 14:58 de serge