我正在尝试在__init__(self)
方法中创建一个元组,但它显示ValueError: Accel.x must have 2 components (got (0, 0, 0, 0, 0))
以下是代码:
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 :(得分: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']