Python pygame.init()不';好像没有人叫装修工来

Python pygame.init()不';好像没有人叫装修工来,python,pygame,decorator,Python,Pygame,Decorator,我一直在尝试将主事件循环放在装饰器中,希望我的程序看起来更整洁。我有两个文件,main.py和event\u loop\u decorator.py 在event\u loop\u decorator.py中: import pygame class EventLoop(object): def __init__(self, func): self.func = func pygame.init() print("pygame shou

我一直在尝试将主事件循环放在装饰器中,希望我的程序看起来更整洁。我有两个文件,
main.py
event\u loop\u decorator.py

event\u loop\u decorator.py
中:

import pygame


class EventLoop(object):
    def __init__(self, func):
        self.func = func
        pygame.init()
        print("pygame should have been initialised.")

    def __call__(self):
        while True:
            pygame.time.wait(50)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    break
            self.func()
import pygame
import event_loop_decorator


@event_loop_decorator.EventLoop
def while_in_event_loop():
    pass


if __name__ == "__main__":
    surface = pygame.display.set_mode((200, 100))
    surface.fill((255, 0, 0))
    pygame.display.flip()
    while_in_event_loop()
main.py
中:

import pygame


class EventLoop(object):
    def __init__(self, func):
        self.func = func
        pygame.init()
        print("pygame should have been initialised.")

    def __call__(self):
        while True:
            pygame.time.wait(50)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    break
            self.func()
import pygame
import event_loop_decorator


@event_loop_decorator.EventLoop
def while_in_event_loop():
    pass


if __name__ == "__main__":
    surface = pygame.display.set_mode((200, 100))
    surface.fill((255, 0, 0))
    pygame.display.flip()
    while_in_event_loop()
如您所见,这个简单的程序旨在显示一个红色的200*100小窗口。它工作正常,退出窗口按钮似乎工作正常。但在程序退出后,我得到以下输出和错误

pygame should have been initialised.
Traceback (most recent call last):
  File "C:/Users/ray/Dropbox/Programming/Python/pygame_test/code/main.py", line 50, in <module>
    while_in_event_loop()
  File "C:\Users\ray\Dropbox\Programming\Python\pygame_test\code\event_loop_decorator.py", line 13, in __call__
    for event in pygame.event.get():
pygame.error: video system not initialized

Process finished with exit code 1
pygame应该已经初始化。
回溯(最近一次呼叫最后一次):
文件“C:/Users/ray/Dropbox/Programming/Python/pygame_test/code/main.py”,第50行,在
而在事件循环()中
文件“C:\Users\ray\Dropbox\Programming\Python\pygame\u test\code\event\u loop\u decorator.py”,第13行,在调用中__
对于pygame.event.get()中的事件:
pygame.error:视频系统未初始化
进程已完成,退出代码为1
我们看到decorator的构造函数中的print命令被调用,它是输出的第一行。但在后来的输出中,我们看到明显的“视频系统未初始化”。当我在没有装饰师的情况下做这件事时,一切都很顺利。顺便说一下,这是我第一次使用装饰器


有什么帮助吗?我和装修工有什么不对劲吗?
pygame.init()
是否永远不要与装饰器一起使用?

如果您查看一下您的
\uuuu调用\uuuuu
函数,您会发现当您中断时,您会再次返回while循环。从函数返回将解决此问题

def __call__(self):
    while True:
        pygame.time.wait(50)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
        self.func()