Python 3 에서 PyOpenGL의 글쇠 먹통 문제

andysheep의 이미지

시험 환경은 64비트 데브원 리눅스

아래처럼 글쇠값 처리하면 key에 들어오는 값은 bytes라 if 문이 엉뚱한 짓을 해
글쇠 함수가 먹통이 됩니다. PyOpenGL 데모 예제 몽땅 글쇠 처리 못하는 버그가 있어요.

def keyboard(key, x, y):
    # Allow to quit by pressing 'Esc' or 'q'
    if key == chr(27):
        sys.exit()
    if key == 'q':
        sys.exit()

위 코드에 다음 코드 두 줄 넣고 돌려본 출력 예

ch = key.decode("utf-8")
print(type(key), key, type(ch), ch)

key는 bytes, ch는 유니코드로 변환된 string 값

<class 'bytes'> b'a' <class 'str'> a
 
<class 'bytes'> b'b' <class 'str'> b
 
<class 'bytes'> b'c' <class 'str'> c
 
<class 'bytes'> b'1' <class 'str'> 

아래처럼 bytes로 들어오는 글쇠 값을 string으로 변환해줍니다.

def keyboard(bkey, x, y):
    # Convert bytes object to string 
    key = bkey.decode("utf-8")
    # Allow to quit by pressing 'Esc' or 'q'
    if key == chr(27):
        sys.exit()
    if key == 'q':
        print("Bye!")
        sys.exit()

Forums: