我一直试图找出这段代码的错误:
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.clock import Clock
from plyer.facades import accelerometer
class Accel(Widget):
def __init__(self):
super().__init__()
self.sensorEnabled = False
def get_acceleration(self, dt):
val = accelerometer.acceleration
self.ids.label1.text = "X: " + str(val[0])
self.ids.label2.text = "Y: " + str(val[1])
self.ids.label3.text = "Z: " + str(val[2])
def pressed1(self):
try:
if not self.sensorEnabled:
# accelerometer.enable()
Clock.schedule_interval(self.get_acceleration, 1 / 20.)
self.sensorEnabled = True
self.ids.button1.text = "Stop"
else:
# accelerometer.disable()
Clock.unschedule(self.get_acceleration)
self.sensorEnabled = False
self.ids.button1.text = "Start"
except:
self.ids.status.text = "Accelerometer not supported"
class e3App(App):
def build(self):
return Accel()
if __name__ == '__main__':
e3App().run()
当我运行它并按下START按钮时它会崩溃并给我这个:
> Traceback (most recent call last): File
> "/usr/local/lib/python3.5/dist-packages/plyer/platforms/linux/accelerometer.py",
> line 22, in _get_acceleration
> pos = glob.glob("/sys/devices/platform/*/position")[0] IndexError: list index out of range During handling of the above
> exception, another exception occurred: Traceback (most recent call
> last): File
> "/home/rickdps/Desktop/KivyCookBook/InputMotionAndTouch/WorkingWithAccelerometer/main.py",
> line 42, in <module>
> e3App().run() File "/usr/local/lib/python3.5/dist-packages/kivy/app.py", line 828, in run
> runTouchApp() File "/usr/local/lib/python3.5/dist-packages/kivy/base.py", line 504, in
> runTouchApp
> EventLoop.window.mainloop() File "/usr/local/lib/python3.5/dist-packages/kivy/core/window/window_sdl2.py",
> line 663, in mainloop
> self._mainloop() File "/usr/local/lib/python3.5/dist-packages/kivy/core/window/window_sdl2.py",
> line 405, in _mainloop
> EventLoop.idle() File "/usr/local/lib/python3.5/dist-packages/kivy/base.py", line 339, in
> idle
> Clock.tick() File "/usr/local/lib/python3.5/dist-packages/kivy/clock.py", line 581, in
> tick
> self._process_events() File "kivy/_clock.pyx", line 367, in kivy._clock.CyClockBase._process_events
> (/tmp/pip-build-rau3eo9p/kivy/kivy/_clock.c:7700) File
> "kivy/_clock.pyx", line 397, in
> kivy._clock.CyClockBase._process_events
> (/tmp/pip-build-rau3eo9p/kivy/kivy/_clock.c:7577) File
> "kivy/_clock.pyx", line 395, in
> kivy._clock.CyClockBase._process_events
> (/tmp/pip-build-rau3eo9p/kivy/kivy/_clock.c:7498) File
> "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
> (/tmp/pip-build-rau3eo9p/kivy/kivy/_clock.c:3490) File
> "/home/rickdps/Desktop/KivyCookBook/InputMotionAndTouch/WorkingWithAccelerometer/main.py",
> line 15, in get_acceleration
> val = accelerometer.acceleration[:3] File "/usr/local/lib/python3.5/dist-packages/plyer/utils.py", line 103, in
> __getattribute__
> return getattr(object.__getattribute__(self, '_obj'), name) File
> "/usr/local/lib/python3.5/dist-packages/plyer/facades/accelerometer.py",
> line 44, in acceleration
> return self.get_acceleration() File "/usr/local/lib/python3.5/dist-packages/plyer/facades/accelerometer.py",
> line 60, in get_acceleration
> return self._get_acceleration() File "/usr/local/lib/python3.5/dist-packages/plyer/platforms/linux/accelerometer.py",
> line 24, in _get_acceleration
> raise Exception('Could not enable accelerometer!') Exception: Could not enable accelerometer!
我正在关注这本名为“#34; Kivy Cook Book&#34;”的书,并且我主要复制和粘贴代码以便尝试。这个没有用过。
我非常感谢任何帮助。