【问题标题】:Error when storing accelerometer data in json file using kivy.使用 kivy 在 json 文件中存储加速度计数据时出错。
【发布时间】:2017-04-02 11:05:19
【问题描述】:

在以下代码中,我按照 kivy 文档中关于将数据存储到 JSON 文件中的说明进行操作。
我收到有关目录的错误。我更新鲜了,但我从过去的日子里被困住了。
错误是 -->

File "main.py", line 44 data_dir = getattr (self, '/storage/emulated/0/') #获取保存文件的可写路径 ^ IndentationError: unexpected indent

__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
from kivy.storage.jsonstore import JsonStore
from os.path import join

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

            data_dir = getattr(self, '/storage/emulated/0/') #get a writable path to save the file
            store = JsonStore(join(data_dir, 'user.json'))   

            store.put('x',accelerometer.acceleration[0])
            store.put('y',accelerometer.acceleration[1])
            store.put('z',accelerometer.acceleration[2])
        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

【问题讨论】:

  • 你能把错误添加到问题中吗?
  • File "main.py", line 28 data_dir = getattr(self, '/storage/emulated/0/') #获取保存文件的可写路径 ^ IndentationError: unexpected indent@EL3PHANTEN
  • 哦,好吧,在这种情况下,您必须修复缩进。我编辑了您的问题以正确缩进。有时当人们发布问题时,他们会弄错缩进。 Aparantly 在你的情况下,你的代码是错误的。立即尝试您问题中的代码。
  • @AbdulRehman 请将错误添加到问题中。
  • @Al.G.上面的评论显示错误。当我使用“buildozer -v android debug”命令运行我的代码时,它会构建,但是当我在 android 中运行 apk 时它会崩溃。

标签: python json python-2.7 kivy


【解决方案1】:

在我的更新函数中,我将错误的参数传递给 json 存储。

jsonStore=('/storage/emulated/0/hello.json')

这是将你的 android 设备的路径传递给 json 存储的方式

json.put('a')

它将把这个字符放在你的android手机内部存储的hello.json中。

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多