为什么要检查 pygame.QUIT,当可以点击 ‘X’ 按钮关闭窗口?

huangapple go评论64阅读模式
英文:

Why is pygame.QUIT being checked when the 'X' button can be clicked and the window would be closed?

问题

I am learning Python's pygame module so I can make games and I'm fairly new to it. I am watching a tutorial and in the following code, when the for loop is checking for a pygame.QUIT event the run variable is turned to false and pygame.quit() quits pygame.

My question is, why is the pygame.QUIT being checked when the 'X' button will close the window?

英文:

I am learning Python's pygame module so I can make games and I'm fairly new to it. I am watching a tutorial and in the following code, when the for loop is checking for a pygame.QUIT event the run variable is turned to false and pygame.quit() quits pygame.

My question is, why is the pygame.QUIT being checked when the 'X' button will close the window?

为什么要检查 pygame.QUIT,当可以点击 ‘X’ 按钮关闭窗口?

答案1

得分: 3

当按下X按钮时,会创建一个事件对象并添加到事件队列中。可以使用pygame.event.get()来检索事件对象。type属性表示事件的类型。如果不检查event.type == pygame.QUIT并且不设置run = False,循环将永远不会结束(无限循环),应用程序将永远不会终止,窗口也不会关闭。pygame.quit()会取消初始化所有pygame模块,这也会取消初始化显示模块并导致窗口关闭。在脚本完成后退出应用程序时,窗口也会关闭。

英文:

When the X button is pressed, an event object is created and added to the event queue. The event objects are retrieved with pygame.event.get(). The type property indicates the type of the event.
If you do not check event.type == pygame.QUIT and do not set run = False, the loop will never end (endless loop), the application will never terminate and the window will not close.
pygame.quit() deinitializes all pygame modules, which also deinitializes the display module and causes the window to close. The window is also closed when the application exits after your script is finished.

答案2

得分: 1

"The "X" button does not automatically force the window to close. All it does is send a message to the window saying "the user would like to close you now". It is entirely up to the application to check for that message (in this case, pygame.QUIT) and take the appropriate action to close itself."

英文:

The "X" button does not automatically force the window to close. All it does is send a message to the window saying "the user would like to close you now". It is entirely up to the application to check for that message (in this case, pygame.QUIT) and take the appropriate action to close itself.

huangapple
  • 本文由 发表于 2023年6月15日 02:46:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定