我刚刚为圣诞节戴上了帽子,我正在浏览以下网站:https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat。在做网站的操纵杆部分时,我输入了以下代码:
from sense_hat import SenseHat
sense = SenseHat()
while True:
for event in sense.stick.get_events():
print(event.direction, event.action)
并收到以下错误:
Traceback (most recent call last):
File "/home/pi/python_programmes/hat_short.py", line 4, in <module>
for event in sense.stick.get_events():
AttributeError: 'SenseHat' object has no attribute 'stick'
有人可以帮我吗?
答案 0 :(得分:0)
根据source code,最新版本(截至2016年6月)在stick
类上有SenseHat
属性。确保您安装了最新的sense-hat
:
sudo apt-get install --only-upgrade sense-hat
如果这不起作用,您可以克隆存储库并手动安装:
git clone https://github.com/RPi-Distro/python-sense-hat
cd python-sense-hat
sudo python setup.py install
答案 1 :(得分:0)
有一个名为https://www.element14.com/community/community/raspberry-pi/raspberry-pi-accessories/blog/2017/01/23/raspberry-pi-sense-hat-enabling-the-joystick的网站,它带您了解如何使操纵杆工作,以及如何制作响应移动操纵杆的程序,在屏幕上移动一个点。
要使操纵杆工作,请在终端中键入以下内容:
cd /usr/lib/python2.7/dist-packages/sense_hat
然后:
sudo rm __init__.py sense_hat.py __init__.pyc sense_hat.pyc
然后:
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/__init__.py
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/sense_hat.py
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/stick.py
然后:
sudo nano sense_hat.py
然后查看第17行,代码应如下所示:
from .stick import SenseStick
但是您需要删除点,第17行应该如下所示:
from .stick import SenseStick
然后你需要按ctrl + o然后输入保存更改并按ctrl + x退出nano
使用操纵杆的代码示例:
from sense_hat import SenseHat
sense = SenseHat()
while True:
for x in sense.stick.get_events():
if x.direction == 'up':
sense.show_letter("U")
elif x.direction == 'down':
sense.show_letter("D")
elif x.direction == 'left':
sense.show_letter("L")
elif x.direction == 'right':
sense.show_letter("R")
elif x.direction == 'middle':
sense.show_letter("M")