2017-07-24 116 views
1

我想創建一個__init__(self)方法裏面一個元組asigning元組卻是露出ValueError: Accel.x must have 2 components (got (0, 0, 0, 0, 0))得到錯誤,而在Python

下面是代碼:

from kivy.app import App 
from kivy.lang import Builder 
from kivy.clock import Clock 
from plyer import accelerometer 
from kivy.uix .relativelayout import RelativeLayout 
Builder.load_string(""" 
<Accel>: 
    BoxLayout: 
     orientation:'vertical' 
     Label: 
      id: x_val 
      text: 'X:' 
     Label: 
      id: y_val 
      text: 'y:' 
     Label: 
      id: z_val 
      text: 'z:' 

     Label: 
      id: x_tst 
      text: 'value:' 

    BoxLayout: 
     size_hint_y: None 
     height: '48dp' 
     padding: '4dp' 

     ToggleButton: 
      id: start_btn 
      text: 'Start accelerometer' 
      on_press: root.accelerometer() 
""") 

class Accel(RelativeLayout): 


    def __init__(self): 
     super(Accel, self).__init__() 
     self.sensorEnabled=False 
     self.counter=0 
     self.x=(0,0,0,0,0) 

    def accelerometer(self): 


     if not self.sensorEnabled: 
      accelerometer.enable() 
      Clock.schedule_interval(self.accelerate, 1/5) 
      self.sensorEnabled =True 
      self.ids.start_btn.text="Stop" 
     else: 
      accelerometer.disable() 
      Clock.unschedule(self.accelerate) 
      self.sensorEnabled =False 
      self.ids.start_btn.text = "Start" 

    def accelerate(self,dt): 
     print(self.x) 
     val=accelerometer.acceleration[:3] 


     if not val==(None,None,None): 
      self.ids.x_val.text="X:" +str(val[0]) 
      self.ids.y_val.text="y:" +str(val[1]) 
      self.ids.z_val.text="z:" +str(val[2]) 




class MeterApp(App): 
    def build(self): 
     return Accel() 

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

當我運行它,它表明:

File "/root/PycharmProjects/Chat/accelerometer.py", line 73, in build 
    return Accel() 
    File "/root/PycharmProjects/Chat/accelerometer.py", line 42, in __init__ 
    self.x=(0,0,0,0,0) 
    File "kivy/properties.pyx", line 478, in kivy.properties.Property.__set__ (/tmp/pip-build-0vou9szt/kivy/kivy/properties.c:5572) 
    File "kivy/properties.pyx", line 498, in kivy.properties.Property.set (/tmp/pip-build-0vou9szt/kivy/kivy/properties.c:6091) 
    File "kivy/properties.pyx", line 625, in kivy.properties.NumericProperty.convert (/tmp/pip-build-0vou9szt/kivy/kivy/properties.c:7891) 
ValueError: Accel.x must have 2 components (got (0, 0, 0, 0, 0)) 

Process finished with exit code 1 

我該如何擺脫這個問題?

+0

當我嘗試添加self.x = [0,0,0, 0,0]它向我展示了同樣的錯誤 –

+0

請儘量保持聊天問題的最小化,特別是在標題中 - 「這裏是代碼」和「看一看」在這裏是相當多的。 – halfer

+0

你能把你的代碼降到最低嗎?看起來好像有很多東西與你的問題完全無關。此外,錯誤似乎告訴你,'Accel.x'應該只有2個組件(例如'(0,0)'),而不是5個。 –

回答

1

您收到錯誤,因爲self.x已經是RelativeLayout的位置屬性,並且類型爲int。所以你需要給你的元組命名一些東西。

編輯:

只是爲了顯示你,print(dir(self)),你會得到( 'X' 是在結尾):

['__class__', '__delattr__', '__dict__', '__doc__', '__events__', '__format__', '__getattribute__', '__hash__', 
'__init__', '__metaclass__', '__module__', '__new__', '__proxy_getter', '__proxy_setter', '__pyx_vtable__', '__reduce__', 
'__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_apply_transform', 
'_context', '_kwargs_applied_init', '_proxy_ref', '_trigger_layout', '_walk', '_walk_reverse', 'accelerate', 
'accelerometer', 'add_widget', 'apply_property', 'bind', 'canvas', 'center', 'center_x', 'center_y', 'children', 
'clear_widgets', 'cls', 'collide_point', 'collide_widget', 'counter', 'create_property', 'disabled', 'dispatch', 
'dispatch_children', 'dispatch_generic', 'do_layout', 'events', 'export_to_png', 'fbind', 'funbind', 'get_center_x', 
'get_center_y', 'get_parent_window', 'get_property_observers', 'get_right', 'get_root_window', 'get_top', 
'get_window_matrix', 'getter', 'height', 'id', 'ids', 'is_event_type', 'layout_hint_with_bounds', 'on_disabled', 
'on_opacity', 'on_touch_down', 'on_touch_move', 'on_touch_up', 'opacity', 'parent', 'pos', 'pos_hint', 'properties', 
'property', 'proxy_ref', 'register_event_type', 'remove_widget', 'right', 'sensorEnabled', 'set_center_x', 'set_center_y', 
'set_right', 'set_top', 'setter', 'size', 'size_hint', 'size_hint_max', 'size_hint_max_x', 'size_hint_max_y', 
'size_hint_min', 'size_hint_min_x', 'size_hint_min_y', 'size_hint_x', 'size_hint_y', 'to_local', 'to_parent', 'to_widget', 
'to_window', 'top', 'uid', 'unbind', 'unbind_uid', 'unregister_event_types', 'walk', 'walk_reverse', 'width', 'x', 'y'] 
+0

謝謝你的答案。它確實有幫助 –