0
我用jsonstorage和pickle,但它沒有工作。也許我做錯了方式?活動出現,然後消失。日誌說處理程序錯誤。在update()
函數中存儲的值爲txt
變量。我想將此txt
存儲到我的手機上的外部文件中。如何使用python kivy在我的android設備上的文件中保存加速度數據?
我的代碼:
__version__ = '1.0' # declare the app version. Will be used by buildozer
from kivy.app import App # for the main app
from kivy.uix.floatlayout import FloatLayout # the UI layout
from kivy.uix.label import Label # a label to show information
from plyer import accelerometer # object to read the accelerometer
from kivy.clock import Clock # clock to schedule a method
class UI(FloatLayout): # the app ui
def __init__(self, **kwargs):
super(UI, self).__init__(**kwargs)
self.lblAcce = Label(text="Accelerometer: ") # create a label at the center
self.add_widget(self.lblAcce) # add the label at the screen
try:
accelerometer.enable() # enable the accelerometer
# if you want do disable it, just run: accelerometer.disable()
Clock.schedule_interval(self.update, 2.0/1) # 24 calls per second
except:
self.lblAcce.text = "Failed to start accelerometer" # error
def update(self, dt):
txt = ""
try:
txt = "Accelerometer:\nX = %.2f\nY = %.2f\nZ = %2.f " %(
accelerometer.acceleration[0], # read the X value
accelerometer.acceleration[1], # Y
accelerometer.acceleration[2]) # Z
except:
txt = "Cannot read accelerometer!" # error
self.lblAcce.text = txt # add the correct text
class Accelerometer(App): # our app
def build(self):
ui = UI() # create the UI
return ui # show it
if __name__ == '__main__':
Accelerometer().run() # start our app