我一直在写一个竞赛条目,试图用Raspberry Pi Sense HAT制作一个MP3播放器。我添加了一个带pygame.mixer.music
的播放功能,我希望在屏幕上显示模式(从选择中随机生成),但是当我运行程序时,我只会收到此错误:
Traceback (most recent call last):
File "/home/pi/Astro Pi/MP3 Player/player.py", line 90, in <module>
display()
File "/home/pi/Astro Pi/MP3 Player/player.py", line 65, in display
sense.set_pixels(pattern_one)
File "/usr/lib/python2.7/dist-packages/sense_hat/sense_hat.py", line 268, in set_pixels
if len(pix) != 3:
TypeError: object of type 'int' has no len()
你能帮忙吗?这是我的完整代码...
#! /usr/bin/env python
# Import the SenseHAT libraries and set them up for easy access
from sense_hat import SenseHat
sense = SenseHat()
# Import the other libraries I need for the MP3 player
import time, os, pygame.mixer, random, glob
# Make sure that the music will play out of the Audio Jack socket
def display():
colors = [[178,31,53],[216,39,53],[255,116,53],[255,161,53],[255,203,53],[255,249,53],[0,117,58],[0,158,71],[22,221,52],[0,82,165],[0,121,231],[0,169,252],[104,30,126],[125,60,181],[189,122,246]]
a = random.choice(colors)
b = random.choice(colors)
c = random.choice(colors)
choice = random.randint(1,5)
pattern_one = [
a,0,0,b,b,0,0,a,
0,a,0,b,b,0,a,0,
0,0,a,b,b,a,0,0,
b,b,b,a,a,b,b,b,
b,b,b,a,a,b,b,b,
0,0,a,b,b,a,0,0,
0,a,0,b,b,0,a,0,
a,0,0,b,b,0,0,a
]
pattern_two = [
a,b,a,b,a,b,a,b,
b,a,b,a,b,a,b,a,
a,b,a,b,a,b,a,b,
b,a,b,a,b,a,b,a,
a,b,a,b,a,b,a,b,
b,a,b,a,b,a,b,a,
a,b,a,b,a,b,a,b,
b,a,b,a,b,a,b,a
]
pattern_three = [
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b,
a,b,a,b,a,b,a,b
]
pattern_four = [
a,a,a,a,a,a,a,a,
b,b,b,b,b,b,b,b,
a,a,a,a,a,a,a,a,
b,b,b,b,b,b,b,b,
a,a,a,a,a,a,a,a,
b,b,b,b,b,b,b,b,
a,a,a,a,a,a,a,a,
b,b,b,b,b,b,b,b,
]
pattern_five = [
a,a,b,b,b,b,a,a,
a,a,b,b,b,b,a,a,
a,a,c,c,c,c,a,a,
a,a,c,c,c,c,a,a,
a,a,b,b,b,b,a,a,
a,a,b,b,b,b,a,a,
a,a,c,c,c,c,a,a,
a,a,c,c,c,c,a,a
]
if choice == 1:
sense.set_pixels(pattern_one)
elif choice == 2:
sense.set_pixels(pattern_two)
elif choice == 3:
sense.set_pixels(pattern_three)
elif choice == 4:
sense.set_pixels(pattern_four)
elif choice == 5:
sense.set_pixels(pattern_five)
time.sleep(0.2)
sense.clear()
os.system("sudo amixer cset numid=3 1")
# Make sure that the volume is turned up so that pygame can control it
os.system("sudo amixer cset numid=1 100%")
# Initialise pygame
pygame.mixer.init()
# Look for all the .mp3 files in the 'songs' folder and put their file names in a list
songs = glob.glob('songs/' + '*.mp3')
# Choose a random file from the list and load it up to start with
pygame.mixer.music.load(random.choice(songs))
# Make sure the volume is up to 100%
pygame.mixer.music.set_volume(1.0)
# Play the song
pygame.mixer.music.play()
while True:
display()
答案 0 :(得分:0)
我认为,因为你在模式矩阵中使用了零,所以需要使用RGB列表。尝试将这些零设置为某种颜色(我猜是黑色),就像在此参考中一样: https://pythonhosted.org/sense-hat/api/#set_pixels
from sense_hat import SenseHat
sense = SenseHat()
X = [255, 0, 0] # Red
O = [255, 255, 255] # White
question_mark = [
O, O, O, X, X, O, O, O,
O, O, X, O, O, X, O, O,
O, O, O, O, O, X, O, O,
O, O, O, O, X, O, O, O,
O, O, O, X, O, O, O, O,
O, O, O, X, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, X, O, O, O, O
]
sense.set_pixels(question_mark)