2017-04-02 23 views
1

在下面的代碼中,我按照關於將數據存入JSON文件的kivy文檔的說明。
我收到有關目錄的錯誤。我更新鮮,我從過去的日子裏陷入困境。
錯誤是 - >使用kivy將加速度計數據存儲在json文件中時出錯。

文件 「main.py」,第44行DATA_DIR = GETATTR(個體, '/存儲/模擬/ 0 /')#獲取一個可寫的路徑,將文件保存^ IndentationError:意想不到縮進

__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 
+0

你可以添加錯誤的問題? – EL3PHANTEN

+0

文件「main.py」,第28行 data_dir = getattr(self,'/ storage/emulated/0 /')#獲取一個可寫路徑來保存文件 ^ IndentationError:意外縮進@ EL3PHANTEN –

+0

哦okey情況下,你必須修復你的縮進。我編輯了你的問題來獲得縮進權。有時,當人們發問時,他們會得到錯誤的縮進。在你的情況下,你的代碼是錯誤的。現在試試你的問題中的代碼。 – EL3PHANTEN

回答

1

在我的更新函數中,我將錯誤的arrguemnts傳遞給json商店。

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

這是通過您的Android設備的路徑JSON店

json.put('a') 

它會把這個字符hello.json在你的Android手機的內部存儲的方式。

相關問題