[英]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 仍然會立即關閉。 由於我還是一個新手,我很可能打錯了所有的壘,但我不知道我錯過了什么。
這可確保在此特定程序運行時 GUI 將打開
你是對的,但在這種情況下__name__
應該設置為'__main__'
,所以:
if __name__ == "__main__":
Coursework()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.