======Kivy: Comment faire ou ne pas faire un tas de chose ======
**{{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]]**
**[[les_pages_kivy_en_details|Les pages Kivy en détails]]**
===== 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 médiocres 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.
====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:
* **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
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
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()
,
,
,
,
,
,
,
,
,
,
......
=====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:
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 !!!!
===== 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 !**
Voir l'exemple ci-dessous.
===== Comment changer la couleur d'un Rectangle ? =====
**Répond aussi à la Question:**
**Comment définir et accéder aux attributs dans le main et le *.kv ?**
Les attributs des widgets défini dans le *.kv sont accessibles depuis le main, seulement si les attributs du widget sont définis en [[https://python.developpez.com/cours/DiveIntoPython/php/frdiveintopython/object_oriented_framework/class_attributes.php|attribut de classe]].
Exemple extrait de [[https://github.com/sergeLabo/multipong/tree/master/multipong|multipong]]:
***.kv**
:
canvas:
Color:
rgb: self.rect_color
Rectangle:
pos: self.pos
size: root.height/65, root.height/7
:
...
...
paddle_0: paddle0
...
...
PongPaddle: # paddle_0
id: paddle0
pos: self.pos
...
...
BoxLayout:
orientation: 'horizontal'
BoxLayout:
size_hint_x: 0.70
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
points: [int(x*(root.top/720)) for x in root.points]
joint: 'miter'
width: 3
close: 1
**extrait de main.py**
...
...
class PongPaddle(Widget):
rect_color = ListProperty([1, 1, 1])
angle = NumericProperty(0)
...
...
class Screen1(Screen):
...
...
def apply_paddle_red_color(self, my_num):
if my_num == 0:
self.paddle_0.rect_color = 1, 0, 0
...
...
=====Button background_color=====
Button:
background_normal: ''
background_color: 1, .3, .4, .85
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]]**
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
===== 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 =====
Dans buildozer.spec, définir:
android.permissions = INTERNET,CHANGE_WIFI_MULTICAST_STATE,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE
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 =====
* **[[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]]
=====Comment éviter ou résoudre des bugs à la compilation avec buildozer=====
===Comment commenter dans buildozer.spec ?===
Ne pas faire:
requirements = python3,kivy,plyer,numpy,oscpy#,jnius
ni
requirements = python3,kivy,plyer,numpy,oscpy #,jnius
mais
requirements = python3,kivy,plyer,numpy,oscpy
#,jnius
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=====
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()
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
{{tag> kivy python sb }}