Akash Tyagi Akash Tyagi - 3 years ago 110
Python Question

Getting error while asigning a tuple in Python

I am trying to create a tuple inside a

__init__(self)
method but it is showing
ValueError: Accel.x must have 2 components (got (0, 0, 0, 0, 0))


Here is the code:

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()


When I run it it shows:

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


How can I get rid of this problem?

Mox Mox
Answer Source

You getting an error because self.x is already a position attribute of RelativeLayout and is of type int. So your going to need to name your tuple something else.

Edit:

Just to show you, print(dir(self)) and you get ('x' is at the end) :

['__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']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download