我已经将动画python程序转换为.exe文件,但是在打开.exe文件时,cmd弹出窗口会在4-5秒后自动关闭

时间:2020-04-20 07:21:38

标签: python pyinstaller

我已经使用python中的pyinstaller模块创建了文件 [这是我在运行.exe文件时遇到的错误] 单击“ 1000”以查看错误图像文件==> 1000

以下是动画文件的代码,我已使用python中的pyinstaller模块将此代码转换为.exe文件

import pygame

pygame.init()

FPS = 60000000000000
fpsClock = pygame.time.Clock()

dis = pygame.display.set_mode((500,500))
white = (255,255,255)
pygame.display.set_caption("Cat Animations !")

catImg = pygame.image.load('cat.png')
catx = 5
caty = 5
direction = 'right'

while True:
    dis.fill(white)

    if direction == 'right':
        catx +=5
        if catx == 270:
            direction = 'down'
    elif direction == 'down':
        caty +=5
        if caty ==270:
            direction = 'left'
    elif direction == 'left':
        catx -=5
        if catx ==5:
            direction = 'up'
    elif direction == 'up':
        caty -=5
        if caty ==5:
            direction = 'right'
    dis.blit(catImg, (catx,caty))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    pygame.display.update()
    fpsClock.tick(FPS)

1 个答案:

答案 0 :(得分:1)

您可以使用相对路径。在您的程序中添加此函数,并将您的路径指定为resource_path("cat.png")。将您的声明更改为catImg = pygame.image.load(resource_path('cat.png'))

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception as e:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)