python - 尝试转换py文件时,Pyinstaller中出现错误?

标签 python error-handling pygame exe

我正在尝试获取一个Python文件,并使用Pyinstaller将其转换为exe文件。但是,当我这样做时,我收到一条错误消息:

Failed to execute python script.


这是pygame代码:

import pygame
pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Auto Maze!")


myfont = pygame.font.SysFont("Arial", 13)

x, y = 168, 100

gameover = False
gameover2 = False
done = False
while not done:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.MOUSEBUTTONDOWN:
            if gameover:
                if button1.collidepoint(event.pos):
                    gameover = False
                    gameover2 = gameover
                    x, y = 168, 100

    screen.fill((0, 0, 0))

    """try:
assert player.colliderect(wall1)
except AssertionError:
pass
except NameError:
pass
else:
death_screen = pygame.display.set_mode((400, 300))
button1 = pygame.draw.rect(death_screen, (0, 0, 255), (200, 200, 30, 30))
if donk:
break"""
    if not gameover:
        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_w]:
            y -= 5
        elif pressed[pygame.K_s]:
            y += 5
        elif pressed[pygame.K_a]:
            x -= 5
        elif pressed[pygame.K_d]:
            x += 5

        player = pygame.draw.rect(screen, (0, 255, 0), (x, y, 60, 60))
        wall1 = pygame.draw.rect(screen, (255, 0, 0), (400, 0, -150, 300))
        wall2 = pygame.draw.rect(screen, (255, 0, 0), (0, 0, 150, 300))
        if player.colliderect(wall1):
            gameover = True
        elif player.colliderect(wall2):
            gameover = True
        else:
            gameover = False

    else:
        button1 = pygame.draw.rect(screen, (0, 0, 255), (175, 100, 60, 30))
        text = myfont.render("Try Again", False, (255, 0, 0))
        screen.blit(text, (176, 107 ))

    pygame.display.flip()
    clock.tick(60)

quit()
这是我键入的用于转换文件的内容:pyinstaller --onefile -w "import pygame".py另外,如果有替代Pyinstaller的更好的选择,请告诉我。我听说Pyinstaller并不是最好的。
我的操作系统是Windows 10。

最佳答案

您需要将pyinstaller指向.py文件,而不是.py文件的第一行。
例如,如果您的文件名为example.py,则需要编写

pyinstaller --onefile -w example.py

关于python - 尝试转换py文件时,Pyinstaller中出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61433088/

相关文章:

python - 如何在 Windows 中使用 2to3 工具?

python - 获取 "ValueError: I/O operation on closed file"

python - 处理Python错误时遇到问题

validation - 如果我在TextField的 'setState'中调用 'onChange',首个字母会丢失

python - Pygame 制作 Sprite 脸鼠标

python - Numpy:以向量形式重写外积和对角线加法

python - 类型错误 : unsupported operand type(s) for &: 'sage.rings.rational.Rational' and 'int'

c# - 嵌入式 IronPython 中的错误详细信息

python - Python/pygame 中的语法错误

python - 文本大小变化持久性