【问题标题】:How to turn a Python Script into exe如何将 Python 脚本转换为 exe
【发布时间】:2020-11-23 18:12:46
【问题描述】:

我整天都在尝试将我的 python 脚本变成可执行文件。我尝试过使用 cx-Freeze 和 PyInstaller,但都不起作用。我现在对 PyInstaller 很感兴趣,已经放弃了 cx-Freeze。这是我的代码,我需要有人告诉我是否可以将其变成可执行文件:

# Importing libraries
import pygame
import random
import time

# Initializing PyGame
pygame.init()

# Setting a window name
pygame.display.set_caption("Ping Pong")

# Creating a font
pygame.font.init()
font = pygame.font.SysFont(None, 30)
pong_font = pygame.font.SysFont("comicsansms", 75)
winner_font = pygame.font.SysFont("consolas", 50)

# Set the height and width of the screen
window_width = 700
window_height = 500
size = [window_width, window_height]
game_win = pygame.display.set_mode(size)
game_win2 = pygame.display.set_mode(size)


# Creating a messaging system
def message(sentence, color, x, y, font_type, display):
    sentence = font_type.render(sentence, True, color)
    display.blit(sentence, [x, y])


# Creating colors
white = (225, 225, 225)
black = (0, 0, 0)
gray = (100, 100, 100)

# Setting up ball
ball_size = 25


class Ball:
    """
    Class to keep track of a ball's location and vector.
    """

    def __init__(self):
        self.x = 0
        self.y = 0
        self.change_x = 0
        self.change_y = 0


def make_ball():
    ball = Ball()
    # Starting position of the ball.
    ball.x = 350
    ball.y = 250

    # Speed and direction of rectangle
    ball.change_x = 5
    ball.change_y = 5

    return ball


def main():
    # Scores
    left_score = 0
    right_score = 0

    pygame.init()

    # Loop until the user clicks the close button.
    done = False

    ball_list = []

    ball = make_ball()
    ball_list.append(ball)

    # Right paddle coordinates
    y = 200
    y_change = 0
    x = 50
    # Left paddle coordinates
    y1 = 200
    y1_change = 0
    x1 = 650

    while not done:
        
        # --- Event Processing
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    y_change = -7

                elif event.key == pygame.K_s:
                    y_change = 7

                elif event.key == pygame.K_UP:
                    y1_change = -7

                elif event.key == pygame.K_DOWN:
                    y1_change = 7

            elif event.type == pygame.KEYUP:
                y_change = 0
                y1_change = 0

        y += y_change
        y1 += y1_change

        # Preventing from letting the paddle go off screen
        if y > window_height - 100:
            y -= 10
        if y < 50:
            y += 10
        if y1 > window_height - 100:
            y1 -= 10
        if y1 < 50:
            y1 += 10

        # Logic
        for ball in ball_list:
            # Move the ball's center
            ball.x += ball.change_x
            ball.y += ball.change_y

            # Bounce the ball if needed
            if ball.y > 500 - ball_size or ball.y < ball_size:
                ball.change_y *= -1
            if ball.x > window_width - ball_size:
                ball.change_x *= -1
                left_score += 1
            if ball.x < ball_size:
                ball.change_x *= -1
                right_score += 1

            ball_rect = pygame.Rect(ball.x - ball_size, ball.y - ball_size, ball_size * 2, ball_size * 2)

            left_paddle_rect = pygame.Rect(x, y, 25, 75)
            if ball.change_x < 0 and ball_rect.colliderect(left_paddle_rect):
                ball.change_x = abs(ball.change_x)

            right_paddle_rect = pygame.Rect(x1, y1, 25, 75)
            if ball.change_x > 0 and ball_rect.colliderect(right_paddle_rect):
                ball.change_x = -abs(ball.change_x)

            if right_score == 10:
                message("RIGHT PLAYER HAS WON!!", white, 60, 250, winner_font, game_win)
                pygame.display.flip()
                pygame.event.poll()
                time.sleep(5)
                pygame.quit()
                quit()
            elif left_score == 10:
                message("LEFT PLAYER HAS WON!!", white, 60, 250, winner_font, game_win)
                pygame.display.flip()
                pygame.event.poll()
                time.sleep(5)
                pygame.quit()
                quit()

        # Drawing
        # Set the screen background
        game_win.fill(black)

        # Draw the balls
        for ball in ball_list:
            pygame.draw.circle(game_win, white, [ball.x, ball.y], ball_size)

        # Creating Scoreboard
        message("Left player score: " + str(left_score), white, 10, 10, font, game_win)
        message("Right player score: " + str(right_score), white, 490, 10, font, game_win)

        # Drawing a left paddle
        pygame.draw.rect(game_win, white, [x, y, 25, 100])
        # Drawing a right paddle
        pygame.draw.rect(game_win, white, [x1, y1, 25, 100])

        # Setting FPS
        FPS = pygame.time.Clock()
        FPS.tick(60)

        # Updating so actions take place
        pygame.display.flip()


while True:
    game_win2.fill(black)
    pygame.event.poll()     
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    message("Pong", white, 280, 100, pong_font, game_win2)
    if 150 + 100 > mouse[0] > 150 and 350 + 50 > mouse[1] > 350:
        pygame.draw.rect(game_win, gray, [150, 350, 100, 50])
        if click[0] == 1:
            break
    else:
        pygame.draw.rect(game_win, white, [150, 350, 100, 50])

    if 450 + 100 > mouse[0] > 450 and 350 + 50 > mouse[1] > 350:
        pygame.draw.rect(game_win, gray, [450, 350, 100, 50])
        if click[0] == 1:
            pygame.quit()
            quit()
    else:
        pygame.draw.rect(game_win, white, [450, 350, 100, 50])

    message("Start", black, 175, 367, font, game_win2)
    message("Quit", black, 475, 367, font, game_win2)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # Wrap-up
    # Limit to 60 frames per second
    clock = pygame.time.Clock()
    clock.tick(60)

if __name__ == "__main__":
    main()

您无需为代码烦恼那么多。但我需要帮助才能使用 pyinstaller 将其转换为可执行文件。

【问题讨论】:

  • “都不行” = 究竟是什么?
  • @PranavHosangadi 当我打开一个由 cx-Freeze“处理”的可执行文件时,它会打开一秒钟然后立即关闭。 pyinstaller 也是如此。 :(
  • 你可以直接从终端运行它们吗?或者将您的代码记录到文件而不是错误到 st​​derr?
  • @snoopstick,他们希望您在包含 exe 文件的文件夹中打开命令提示符,并从命令提示符运行 exe 文件。看起来您的程序一启动就完成运行,这就是它打开的 cmd 立即关闭的原因。如果单独打开 cmd 和 exe,程序退出后 cmd 会保持打开状态
  • 没错。在终端中运行 EXE... 它应该显示您的错误足够长的时间,您可以将其复制并放入您的问题中

标签: python pyinstaller executable


【解决方案1】:

有几个常见的事情可能会导致pygamepyinstaller 出现问题:

  1. 如果您还没有,请将 pygame 更新为 2.0.0.dev6 (pip install pygame==2.0.0.dev6)。

  2. 在制作.exe 时,请尝试以下操作:pyinstaller -w --onefile main.py 始终将要转换的 .py 文件更改为 main.py -> (-w 没有控制台) 注意:您需要先导航到main.py 路径

  3. .exe 文件将位于 dist 文件夹中,因此路径将无效,请确保将其移动到初始 main.py 所在的同一文件夹中。

  4. 字体也会导致问题,如果问题仍然存在,请尝试完全删除它们。如果一切正常,请考虑更改字体(Arial 一直对我有用),或者将您要使用的字体复制到您的 .exe 所在的文件夹中

【讨论】:

  • 好吧,当我尝试字体的时候它成功了。 Arial 确实有效,还有另一个名为 Consolas 的似乎也有效!
  • 很高兴听到这个消息。如果您想要一些具体的尝试将其 .ttf 放入文件夹中
猜你喜欢
  • 2021-09-25
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 2011-04-07
相关资源
最近更新 更多