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
我該如何擺脫這個問題?
當我嘗試添加self.x = [0,0,0, 0,0]它向我展示了同樣的錯誤 –
請儘量保持聊天問題的最小化,特別是在標題中 - 「這裏是代碼」和「看一看」在這裏是相當多的。 – halfer
你能把你的代碼降到最低嗎?看起來好像有很多東西與你的問題完全無關。此外,錯誤似乎告訴你,'Accel.x'應該只有2個組件(例如'(0,0)'),而不是5個。 –