Я получаю ошибки с клавиатурой pyglet

Я продолжаю получать ошибки в Pyglet после добавления функций клавиатуры:

Я делаю игровой движок на питоне, экспериментирую с Pyglet и открываю gl.

from pyglet.gl import *
import pyglet
import numpy as np

class Triangle:
    def __init__(self):
        self.vertices = pyglet.graphics.vertex_list(3, ('v3f', [-0.6,-0.6,0.0, 0.6,-0.6,0.0, 0.0,0.6,0.0]),
                                                ('c3B', [255,0,0, 0,255,0, 0,0,255]))
class Quad:
    def __init__(self):
        self.indices = [0,1,2,2,3,0]
        self.vertex = [-0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.5,0.5,0.0, -0.5,0.5,0.0]
        self.colour = [0.99,0.10,0.150, 0.9,0.155,0.100, 0.9,0.50,0.20, 0.100,0.50,0.20]                                                     
        self.vertices = pyglet.graphics.vertex_list_indexed(4, self.indices,('v3f',self.vertex),('c3f', self.colour))
class gameWindow(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_minimum_size(320,240)
        glClearColor(.128, .128, .128, .128)
        self.triangle = Triangle()
        self.quad = Quad()
    def on_draw(self):
        self.clear()
        self.quad.vertices.draw(GL_TRIANGLES)
        self.triangle.vertices.draw(GL_TRIANGLES)
    def on_resize(self, width, height):
        glViewport(0, 0, width, height)
    def on_key_press(symbol, modifiers):
        if symbol == pyglet.window.key.W:
            print('A key was pressed')

if __name__ == "__main__":
    window = gameWindow(640,480,"GAME WINDOW", resizable=True)
    pyglet.app.run()

Затем я внезапно получил эти ошибки после добавления функции on_key_pressed:

Traceback (most recent call last):
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\event.py", line 367, in dispatch_event
    if getattr(self, event_type)(*args):
TypeError: on_key_press() takes 2 positional arguments but 3 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\window\win32\__init__.py", line 639, in f
    result = event_handler(msg, wParam, lParam)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\window\win32\__init__.py", line 710, in _event_key
    self.dispatch_event(ev, symbol, modifiers)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\window\__init__.py", line 1232, in dispatch_event
    if EventDispatcher.dispatch_event(self, *args) != False:
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\event.py", line 371, in dispatch_event
    event_type, args, getattr(self, event_type))
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\event.py", line 421, in _raise_dispatch_exception
    (event_type, len(args), descr))
TypeError: on_key_press event was dispatched with 2 arguments, but handler on_key_press at C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\q.py:28 has an incompatible function signature

Я ничего не нашел в Интернете.


person GRPAT    schedule 24.03.2019    source источник


Ответы (1)


Вы забыли параметр self в своем определении on_key_press().

У вас есть это:

def on_key_press(symbol, modifiers):

Но вы должны использовать это вместо этого:

def on_key_press(self, symbol, modifiers):
person John Gordon    schedule 24.03.2019