Outils pour utilisateurs

Outils du site


kivy_plyer

Ceci est une ancienne révision du document !


Kivy: plyer

Installation

sudo pip3 install plyer

Ressources

Très pauvres !

Possiblité d'accès

Android

Accelerometer Audio recording Barometer Battery Bluetooth Brightness Call Camera (taking picture) Compass Email (open mail client) Flash GPS Gravity Gyroscope Humidity IR Blaster Light Native file chooser Notifications Orientation Proximity SMS (send messages) Spatial Orientation Speech to text Storage Path Temperature Text to speech Unique ID Vibrator

Linux

Accelerometer Battery Brightness CPU count Email (open mail client) Native file chooser Notifications Screenshot Storage Path Text to speech Unique ID Wifi

Exemple

Vous voulez quelque chose, faîtes le vous même !

L'exemple Accelerometer Test pour python3 et corrigé:

main.py
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
 
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
 
from plyer import accelerometer
 
class AccelerometerTest(BoxLayout):
    def __init__(self):
        super().__init__()
        self.sensorEnabled = False
 
    def do_toggle(self):
        if not self.sensorEnabled:
            try:
                accelerometer.enable()
                print(accelerometer.acceleration)
                self.sensorEnabled = True
                self.ids.toggle_button.text = "Stop Accelerometer"
            except:
                print("Accelerometer is not implemented for your platform")
 
            if self.sensorEnabled:
                Clock.schedule_interval(self.get_acceleration, 1 / 20)
            else:
                accelerometer.disable()
                status = "Accelerometer is not implemented for your platform"
                self.ids.toggle_button.text = status
        else:
            # Stop de la capture
            accelerometer.disable()
            Clock.unschedule(self.get_acceleration)
 
            # Retour à l'état arrété
            self.sensorEnabled = False
            self.ids.toggle_button.text = "Start Accelerometer"
 
    def get_acceleration(self, dt):
        if self.sensorEnabled:
            val = accelerometer.acceleration[:3]
 
            if not val == (None, None, None):
                self.ids.x_label.text = "X: " + str(val[0])
                self.ids.y_label.text = "Y: " + str(val[1])
                self.ids.z_label.text = "Z: " + str(val[2])
 
class AccelerometerTestApp(App):
    def build(self):
        return AccelerometerTest()
 
    def on_pause(self):
        return True
 
if __name__ == '__main__':
    AccelerometerTestApp().run()
accelerometertest.kv
#:kivy 1.8.0
<AccelerometerTest>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: x_label
            text: 'X: '
        Label:
            id: y_label
            text: 'Y: '
        Label:
            id: z_label
            text: 'Z: '
        Label:
            id: accel_status
            text: ''
        BoxLayout:
            size_hint_y: None
            height: '48dp'
            padding: '4dp'
 
            ToggleButton:
                id: toggle_button
                text: 'Start accelerometer'
                on_press: root.do_toggle()

android.permissions

Exemple Envoi de Sms

main.py
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
 
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import StringProperty
 
from plyer import sms
 
class SmsInterface(BoxLayout):
    pass
 
class IntentButton(Button):
    sms_recipient = StringProperty()
    sms_message = StringProperty()
 
    def send_sms(self, *args):
        sms.send(recipient=self.sms_recipient, message=self.sms_message)
 
class SmsApp(App):
    def build(self):
        return SmsInterface()
 
    def on_pause(self):
        return True
 
if __name__ == "__main__":
    SmsApp().run()
sms.kv
<SmsInterface>:
    orientation: 'vertical'
    BoxLayout:
        size_hint_y: None
        height: sp(30)
        Label:
            text: 'Recipient:'
        TextInput:
            id: recipient
            multiline: False
            on_text_validate: message.focus = True
    BoxLayout:
        Label:
            text: 'Message:'
        TextInput:
            id: message
    IntentButton:
        sms_recipient: recipient.text
        sms_message: message.text
        text: 'Send SMS'
        size_hint_y: None
        height: sp(40)
        on_release: self.send_sms()
buildozer.spec
[app]
title = Sms
package.name = sms
package.domain = org.test
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = 0.3
requirements = python3,kivy,plyer
orientation = portrait
fullscreen = 0
android.arch = armeabi-v7a
android.permissions = READ_PHONE_STATE, SEND_SMS, RECEIVE_SMS, READ_SMS
 
[buildozer]
log_level = 2
warn_on_root = 1

La permission SEND_SMS n'est pas acceptée par Android

01-04 09:55:02.162  3635  3654 I python  : [INFO   ] [Base        ] Leaving application in progress...
01-04 09:55:02.164  3635  3654 I python  :  Traceback (most recent call last):
01-04 09:55:02.164  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/app/main.py", line 34, in <module>
01-04 09:55:02.166  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/app.py", line 855, in run
01-04 09:55:02.167  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/base.py", line 504, in runTouchApp
01-04 09:55:02.169  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/core/window/window_sdl2.py", line 747, in mainloop
01-04 09:55:02.172  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/core/window/window_sdl2.py", line 479, in _mainloop
01-04 09:55:02.174  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/base.py", line 342, in idle
01-04 09:55:02.177  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/base.py", line 327, in dispatch_input
01-04 09:55:02.178  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/base.py", line 293, in post_dispatch_input
01-04 09:55:02.181  3635  3654 I python  :    File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
01-04 09:55:02.184  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/uix/behaviors/button.py", line 179, in on_touch_up
01-04 09:55:02.185  3635  3654 I python  :    File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
01-04 09:55:02.187  3635  3654 I python  :    File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
01-04 09:55:02.188  3635  3654 I python  :    File "kivy/_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
01-04 09:55:02.189  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/kivy/lang/builder.py", line 64, in custom_callback
01-04 09:55:02.191  3635  3654 I python  :    File "/data/data/org.test.sms/files/app/sms.kv", line 23, in <module>
01-04 09:55:02.194  3635  3654 I python  :      on_release: self.send_sms()
01-04 09:55:02.195  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/app/main.py", line 23, in send_sms
01-04 09:55:02.196  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/plyer/facades/sms.py", line 46, in send
01-04 09:55:02.197  3635  3654 I python  :    File "/media/data/3D/projets/sms/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/sms/plyer/platforms/android/sms.py", line 21, in _send
01-04 09:55:02.200  3635  3654 I python  :    File "jnius/jnius_export_class.pxi", line 1044, in jnius.jnius.JavaMultipleMethod.__call__
01-04 09:55:02.203  3635  3654 I python  :    File "jnius/jnius_export_class.pxi", line 766, in jnius.jnius.JavaMethod.__call__
01-04 09:55:02.205  3635  3654 I python  :    File "jnius/jnius_export_class.pxi", line 860, in jnius.jnius.JavaMethod.call_method
01-04 09:55:02.206  3635  3654 I python  :    File "jnius/jnius_utils.pxi", line 91, in jnius.jnius.check_exception
01-04 09:55:02.207  3635  3654 I python  :  jnius.jnius.JavaException: JVM exception occurred: Sending SMS message: uid 10074 does not have android.permission.SEND_SMS.
01-04 09:55:02.208  3635  3654 I python  : Python for android ended.
kivy_plyer.1578128454.txt.gz · Dernière modification : 2020/01/04 09:00 de serge