在Python中设置元组时出错
我试图在在Python中设置元组时出错,python,kivy,Python,Kivy,我试图在\uuuu init\uuuuuuu(self)方法中创建一个元组,但它显示ValueError:Accel.x必须有两个组件(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 Relativ
\uuuu init\uuuuuuu(self)
方法中创建一个元组,但它显示ValueError:Accel.x必须有两个组件(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()
如何解决此问题?由于
self.x
已经是RelativeLayout
的位置属性,并且类型为int
,因此您会收到一个错误。所以你需要给元组起个别的名字
编辑:
只是想告诉你,print(dir(self))
然后你得到('x'在末尾):
当我尝试添加self.x=[0,0,0,0,0]时,它显示了相同的错误。请尽量减少聊天中的问题,尤其是标题-“这是代码”和“看一看”在那里是非常多余的。您能将代码减少到最低限度吗?看起来里面有很多东西和你的问题完全无关。此外,错误似乎告诉您,
Accel.x
应该只有2个组件(例如,(0,0)
),而不是5个组件。@Elmar PeiseThankyou@halfer我不会重复那个错误。谢谢你的回答。这真的很有帮助
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
['__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']