Kivy Beginner, use on_motion for accelerometer

508 views
Skip to first unread message

Nick S

unread,
Jul 11, 2021, 3:48:04 AM7/11/21
to Kivy users support
Hi Peeps,

I'm just starting out with Kivy. I want to get motion sensor data on an android phone and I was thinking that I could use on_motion for this but I can't seem to get it to fire. I've had success with on_touch_down etc, but can't get anything out of on_motion.

My code is below. Am I implementing on_motion wrong? Do I need to investigate something like plyer? Thanks in advance.

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.core.window import Window

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1
        self.size_hint = (0.6, 0.7)
        self.pos_hint = {"center_x":0.5, "center_y":0.5}

        #add widgets to window   
        self.greeting = Label(text="Ready...")
        self.add_widget(self.greeting)

    def on_motion(self, eType, me, **kwargs):
        self.greeting.text = "Motion detected"

class Accelerometer(App):
    def build(self):
        grid = MyGrid()
        Window.bind(on_motion=grid.on_motion)
        return MyGrid()

if __name__ == "__main__":
    Accelerometer().run()

Nick S

unread,
Jul 11, 2021, 6:59:42 AM7/11/21
to Kivy users support
Ooops. I returned the wrong thing from my build method, also the parameters in my on_motion method don't seem to work. I think this is better...

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.core.window import Window

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1
        self.size_hint = (0.6, 0.7)
        self.pos_hint = {"center_x":0.5, "center_y":0.5}

        #add widgets to window   
        self.greeting = Label(text="Ready...")
        self.add_widget(self.greeting)

    def on_motion(self, eType, me, x):
        self.greeting.text = "Motion detected"

class Accelerometer(App):
    def build(self):
        grid = MyGrid()
        Window.bind(on_motion=grid.on_motion)
        return grid

if __name__ == "__main__":
    Accelerometer().run()

The on_motion method actually runs now so I think I've implemented it more or less correctly. It only runs when I touch my phone screen though, not in response to movement. I think this means I should investigate plyer next. Still... any advice greatly appreciated.

Elliot Garbus

unread,
Jul 11, 2021, 12:18:12 PM7/11/21
to kivy-...@googlegroups.com

The GridLayout does not have an event on_motion.  I assume you want to to use the accelerometer on your phone. 

You will need to use plyer.  See: https://github.com/kivy/plyer

The docs: https://plyer.readthedocs.io/en/latest/#

Examples using the accelerometer:  https://github.com/kivy/plyer/tree/master/examples/accelerometer

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e3a2ac40-84a3-4e4a-8ba6-85744a9b071en%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages