【问题标题】:pygame freezes when I try to exit it after running it from IDLE!当我从 IDLE 运行它后尝试退出 pygame 时,它​​会冻结!
【发布时间】:2011-09-20 10:30:30
【问题描述】:

我通过 livewires 包装器运行它,它只是 pygame 和其他 python 模块的训练轮;但是每次我运行它时,它都会执行,当我尝试退出时,它不会响应然后崩溃。

任何关于我如何解决这个问题的意见都会很棒。我的教科书中没有任何输入,所有谷歌似乎都是使用 pygame 本身解决这个问题的结果。

显然pygame和Tkinter似乎冲突了?

提前致谢!

附录 - 这是我试图运行的代码:

from livewires import games

screen_w = 640
screen_h = 480

my_screen = games.Screen (wid, heit)
my_screen.mainloop()

【问题讨论】:

  • 崩溃是什么意思?它会抛出异常或错误吗?此外,pygame 使用 SDL 而不是 Tkinter,并且已知 SDL 需要一段时间才能关闭。
  • 崩溃是指程序没有响应,我必须去任务管理器退出它。另外,事实证明我不应该从 IDLE 运行 pygame,我应该直接从 Python 运行它。不完全确定为什么,我仍然想知道。从 IDLE 运行它可以确保 pygame 程序变得无响应。
  • 嗯,这很奇怪。在 IDLE 之外运行它会发生什么?

标签: python crash pygame python-idle livewires


【解决方案1】:

我同意。您需要将整个程序(要执行的部分,而不是定义等)放入一个while循环中。问题是pygame在IDLE中退出时不会被告知关闭,但在IDLE之外,程序的关闭会覆盖需要关闭 pygame。

这是循环:

done = False
while done==False:
    # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
           done=True # Flag that we are done so we exit this loop

    #Main program here

pygame.quit()

【讨论】:

    【解决方案2】:

    类似问题:Pygame screen freezes when I close it

    我的教科书中没有任何输入 所有谷歌似乎都屈服于 使用 pygame 解决这个问题 自己。

    这些结果可能解决了您遇到的相同问题。这是来自 livewires 的 games.py 文件的相关部分,它在任何地方都没有调用 pygame.quit()

    def handle_events (self):
        """
        If you override this method in a subclass of the Screen
        class, you can specify how to handle different kinds of
        events.  However you must handle the quit condition!
        """
        events = pygame.event.get ()
        for event in events:
            if event.type == QUIT:
                self.quit ()
            elif event.type == KEYDOWN:
                self.keypress (event.key)
            elif event.type == MOUSEBUTTONUP:
                self.mouse_up (event.pos, event.button-1)
            elif event.type == MOUSEBUTTONDOWN:
                self.mouse_down (event.pos, event.button-1)
    
    def quit (self):
        """
        Calling this method will stop the main loop from running and
        make the graphics window disappear.
        """
    
        self._exit = 1
    
    def mainloop (self, fps = 50):
        """
        Run the pygame main loop. This will animate the objects on the
        screen and call their tick methods every tick.
    
        fps -- target frame rate
        """
    
        self._exit = 0
    
        while not self._exit:
            self._wait_frame (fps)
    
            for object in self._objects:
                if not object._static:
                    object._erase ()
                    object._dirty = 1
    
            # Take a copy of the _objects list as it may get changed in place.
            for object in self._objects [:]:
                if object._tickable: object._tick ()
    
            self.tick ()
    
            if Screen.got_statics:
                for object in self._objects:
                    if not object._static:
                        for o in object.overlapping_objects ():
                            if o._static and not o._dirty:
                                o._erase ()
                                o._dirty = 1
    
            for object in self._objects:
                if object._dirty:
                    object._draw ()
                    object._dirty = 0
    
            self._update_display()
    
            self.handle_events()
    
        # Throw away any pending events.
        pygame.event.get()
    

    QUIT 事件只是设置一个标志,使您退出 mainloop 函数中的 while 循环。我猜如果你在 Python 目录中找到这个文件并在mainloop 的最后一行后面加上pygame.quit(),它会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多