[英]How to use super in Pyglet in Window's subclass events?
我想子類化pyglet.window.Window,但是此代碼使我在on_draw()和on_mouse_pressed()中的super異常
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyglet
class Window(pyglet.window.Window):
def __init__(self, *arguments, **keywords):
super(Window, self).__init__(*arguments, **keywords)
def on_draw(self):
super(Window, self).on_draw()
self.clear()
def on_key_press(self, symbol, modifiers):
super(Window, self).on_key_press(symbol, modifiers)
if symbol == pyglet.window.key.A:
print "A"
def on_mouse_press(self, x, y, button, modifiers):
super(Window, self).on_mouse_press(x, y, button, modifiers)
if button:
print button
window = Window()
pyglet.app.run()
而此代碼沒有。 為什么會這樣呢? 在Pyglet事件中安全使用超級嗎?
pyglet.window.Window中的默認on_draw()調用flip(),但是我無法調用pyglet.window.Window的on_draw(),我也找不到在Pyglet模塊中定義on_draw()的位置。 這在哪里定義,為什么我不能用super調用pyglet.window.Window的on_draw()?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyglet
class Window(pyglet.window.Window):
def __init__(self, *arguments, **keywords):
super(Window, self).__init__(*arguments, **keywords)
def on_draw(self):
self.clear()
def on_key_press(self, symbol, modifiers):
super(Window, self).on_key_press(symbol, modifiers)
if symbol == pyglet.window.key.A:
print "A"
def on_mouse_press(self, x, y, button, modifiers):
if button:
print button
window = Window()
pyglet.app.run()
沒有on_draw
或on_mouse_press
用於定義函數pyglet.window.Window
,所以你不能給他們打電話。 在基類中,它們僅作為已注冊的事件類型存在,這意味着當窗口通過dispatch_event()
接收到其中一個時,它將首先在其事件堆棧中檢查處理程序,然后在其自己的屬性中查找名稱匹配的函數。 如果兩個地方都沒有匹配項,則將忽略該事件。 這就是您可以像在代碼中那樣直接定義處理程序的方法,或者通過在類外部使用裝飾器將處理程序推入堆棧(或兩者)的方法。
EventLoop
確實在每次迭代期間將on_draw
事件調度到每個窗口,但是flip()
被調用。
資源:
class EventLoop(event.EventDispatcher):
# ...
def idle(self):
# ...
# Redraw all windows
for window in app.windows:
if redraw_all or (window._legacy_invalid and window.invalid):
window.switch_to()
window.dispatch_event('on_draw')
window.flip()
window._legacy_invalid = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.