PyOpenGL运行报错ValueError:期望12字节数组却得到8字节数组
Hey, I spotted the exact issue causing your error! Let's break this down:
What's Going Wrong?
When you run your cube code, you hit this frustrating error:
ValueError: ('Expected 12 byte array, got 8 byte array', (-1, 0), None)
Why This Happens
Look closely at your vertices array—there's a tiny syntax mistake in one of the tuples:
vertices = ((1, -1, -1), (1, 1, -1), (-1, 1 -1), (-1, -1, -1), ...)
The third entry (-1, 1 -1) is missing a comma between 1 and -1. Python interprets 1 -1 as a single value (0), so this tuple becomes (-1, 0)—only two elements instead of the three glVertex3fv expects. Since each float in the vertex takes 4 bytes, 2 elements equal 8 bytes, but OpenGL needs 3 elements (12 bytes) for a 3D vertex. That's exactly why you get this mismatch error.
The Simple Fix
Just add the missing comma to that vertex tuple:
# Corrected vertices array vertices = ( (1, -1, -1), (1, 1, -1), (-1, 1, -1), # Added comma here between 1 and -1 (-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1), )
Test It Out
After fixing that line, re-run your code. OpenGL will now read all 3 coordinates for every vertex correctly, and your cube should render without any issues.
内容的提问来源于stack exchange,提问作者Dev

