我无法将py文件打包为exe文件

时间:2020-10-30 13:06:10

标签: python pygame exe pyinstaller

我对编程非常陌生,因此决定使用python作为我的第一门编程语言!因此,我通过Pygame创建了一个没有其他文件的游戏,它只是一个python文件。

这只是一个问答游戏,我已经通过代码本身创建了一个GUI。当使用cmd来运行游戏时,该游戏运行良好,但是当我使用pyinstaller将其转换为可执行文件并尝试运行它时,它只会闪烁一个空白的黑屏,然后关闭。我什至尝试使用cx冻结。我尝试了很多解决方案,但没有一个起作用。任何帮助,将不胜感激。我对编程非常陌生,所以如果您可以回答我的问题,请用初学者的话哈哈哈解释一下。这是我的游戏代码:

import random 
from random import randint

WIDTH = 1280

HEIGHT = 720

def draw():
    screen.fill("green yellow")
    screen.draw.filled_rect(main_box, "sky blue")
    screen.draw.filled_rect(timer_box, "sky blue")
    screen.draw.text("Score: " + str(score), color="black", topleft=(10, 10))

    for box in answer_boxes:
        screen.draw.filled_rect(box, "orange")

    screen.draw.textbox(str(time_left), timer_box, color=('black'))
    screen.draw.textbox(question[0], main_box, color=('black'))

    index = 1
    for box in answer_boxes:
        screen.draw.textbox(question[index], box, color=('black'))
        index = index + 1

def game_over():
    global question, time_left, scoredecreaserps
    scoredecreaserps = 0
    message = 'Game Over. You got %s questions correct' %str(numques)
    question = [message, '-', '-', '-', '-', 5]
    time_left = 0

def correct_answer():
    global question, score, numques, time_left

    numques = numques + 1
    score = score + 1000

    if questions:
        question = questions.pop()
        time_left = 10
    else:
        print('End of questions')
        game_over()

def on_mouse_down(pos):
    index = 1

    for box in answer_boxes:

        if box.collidepoint(pos):
            print('Clicked on answer' + str(index))

            if index == question[5]:
                print('You got it correct!')
                correct_answer()
            else:
                game_over()

        index = index + 1

def update_time_left():
    global time_left

    if time_left:
        time_left = time_left - 1
    else:
        game_over()

def score_lower():
    global score

    if score:
        score = score - scoredecreaserps

main_box = Rect(50, 40, 820, 240)
timer_box = Rect(990, 40, 240, 240)

answer_box1 = Rect(50, 358, 495, 165)
answer_box2 = Rect(735, 358, 495, 165)
answer_box3 = Rect(50, 538, 495, 165)
answer_box4 = Rect(735, 538, 495, 165)

answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]

scoredecreaserps = 80
numques = 0
score = 0
time_left = 10

q1 = ["Who was the third president of the Philippines?",
      'Rodrigo Duterte', 'Ramon Magsaysay', 'Jose P. Laurel',
      'Fidel V. Ramos', 3]

q2 = ['When was the Philippines granted independece from the US?'
      , '1977', '1946', '1935', '1907', 2]

q3 = ['When was the Philippines colonized by Spain', '1898', '1523', '1654',
      '1521', 4]

questions = [q1, q2, q3]

random.shuffle(questions)

question = questions.pop(0)
clock.schedule_interval(update_time_left, 1.0)
clock.schedule_interval(score_lower, 1.0)

3 个答案:

答案 0 :(得分:0)

在这种情况下,我使用auto-py-to-exe https://pypi.org/project/auto-py-to-exe/

我的系统:Windows 8.1上的Python 2.7 人们告诉我该exe文件也很好 使用Linux + Wine

答案 1 :(得分:0)

如果您不熟悉编程,则不应专注于创建二进制文件。通过学习每种代码方法的面向对象编程,函数式编程,优缺点,来学习如何在Shell中运行应用程序并提高您的开发人员技能。

您将意识到大多数项目将不需要exe即可运行它。如果要单击并运行文件,则可以使用某些Windows配置*直接运行python文件,或者创建一个shell脚本来设置和/或运行它。这种方法是使用python的更自然的方法。

*我已经很长时间没有使用Windows了,但是我相信您可以从UI使用python.exe打开python文件。

答案 2 :(得分:0)

我已设法解决您的任务,请执行以下步骤:

  1. 必需的模块需要安装一次:
python -m pip install pyinstaller pgzero requests
  1. 通过运行下一个命令行来检索存档game.zip和所有必需的资源:
python -c "import requests, base64; f = open('game.zip', 'wb'); f.write(base64.b64decode(requests.get('https://pastebin.com/raw/GnJ72zgP').content)); f.close()"
  1. game.zip文件中已经有所有需要的内容,只需在其中运行package.cmd,打包后花一些时间,您将在dist/script.exe中得到生成的exe文件。

  2. game.zip有下一个文件:

    • package.cmd包含一行pyinstaller --noconfirm --onefile --console --noupx --add-data "./pgzero/;./pgzero/" --add-data "./fonts/;./fonts/" --runtime-tmpdir "c:/temp/" script.py
    • script.py包含完整的脚本代码,我将在下面提供。
    • fonts/arial.ttf包含来自C:\Windows\Fonts\arial.ttf的文件。
    • pgzero/data/等于文件夹D:\bin2\Python37\Lib\site-packages\pgzero\data\(在此路径中,更改Python发行版的位置)。

完整更正和改进的脚本代码如下。您可能需要更改第一行-变量logname等于要写入日志的路径,如果程序崩溃,所有异常都写入那里,变量fontname等于位于{{ 1}}中的./fonts/arial.ttf中,您可以用自己喜欢的字体替换。

可以通过game.zip命令运行脚本,也可以使用python script.py将脚本打包到script.exe中。

接下来的改进是

  1. 添加了pyinstaller / try全局块以便捕获所有程序的异常/错误,except块内的错误被保存到日志文件中,该文件的路径为except变量。
  2. 在脚本的第一行中添加了logname,以便捕获faulthandler之类的所有严重错误。
  3. 在第一行中添加了Segmentation Fault,以将工作目录更改为运行打包脚本的目录。因为默认情况下,打包的脚本工作目录不等于脚本所在的目录。
  4. 在第一行中添加了os.chdir(...),在最后一行中添加了import pgzrun,这使得脚本可以仅由pgzrun.go()运行,而不是{{ 1}}脚本正在运行。为了使使用pyinstaller易于打包,必须进行此改进。
  5. python script.py函数的所有调用中添加了pgzrun script.py参数,因为如果没有给出字体文件名,打包的脚本就会崩溃。
pygame