为什么我的 pygame window 立即关闭?

Why does my pygame window instantly close?

当我运行这段代码时:

import pygame
import time
pygame.init()


WIDTH, HEIGHT = 900, 500  # this declares the size of the window the GUI will open in
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Encryption and Decryption")  # this titles the window
FPS = 60  # this declares the  magnitude of the framerate of the window


def draw_window():
    GREY = (54, 45, 45)
    WIN.fill(GREY)  # this colours the window grey
    pygame.display.update()  # this updates the colouring of the window the next time the frame cycles


def Coursework():
    clock = pygame.time.Clock()  # this declares the refresh rate as a variable the function can use
    run = True
    while run:  # everything in this loop will only happen when the window is open
        clock.tick(FPS)  # this refreshes the frame every 1/FPS second
        for event in pygame.event.get():  # this manages events that may happen in the window
            if event.type == pygame.QUIT:
                run = False  # this ends the loop if the user quits the program
        draw_window()  # this ensures that when this function is called the window is created
    pygame.quit()


if __name__ == "__Coursework__":  # this ensures that the GUI will open when this specific program is running
    Coursework()

pygame window 立即关闭。我在另一个类似的问题中看到,应用程序循环必须做某些事情,我相信我已经完成了所有这些事情,但是 window 仍然会立即关闭。由于我还是个新手,我很可能认为我打了所有的垒是错误的,但我不知道我错过了什么。

this ensures that the GUI will open when this specific program is running

你是对的,但在这种情况下 __name__ 应该设置为 '__main__',所以:

if __name__ == "__main__":
    Coursework()