3 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您是編程新手,則不應(yīng)專注于創(chuàng)建二進(jìn)制文件。通過學(xué)習(xí)面向?qū)ο缶幊?、函?shù)式編程以及每種代碼方法的優(yōu)缺點(diǎn),了解如何在 shell 中運(yùn)行應(yīng)用程序并培養(yǎng)一些開發(fā)人員技能。
您將意識(shí)到大多數(shù)項(xiàng)目不需要 exe 來運(yùn)行它。如果您希望文件單擊并運(yùn)行,您可以使用某些 Windows 配置直接運(yùn)行 python 文件*,或者創(chuàng)建一個(gè) shell 腳本來設(shè)置和/或運(yùn)行它。這種方法是使用 python 的更自然的方式。
* 我很長(zhǎng)時(shí)間不使用 Windows,但我相信您可以從 UI 中使用 python.exe 打開 python 文件。

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
我已經(jīng)成功解決了您的任務(wù),請(qǐng)執(zhí)行后續(xù)步驟:
必要的模塊需要一次性安裝:
python -m pip install pyinstaller pgzero requests
game.zip
通過運(yùn)行下一個(gè)命令行來檢索包含所有所需資源的存檔:
python -c "import requests, base64; f = open('game.zip', 'wb'); f.write(base64.b64decode(requests.get('https://pastebin.com/raw/GnJ72zgP').content)); f.close()"
文件內(nèi)部
game.zip
已經(jīng)有所需的一切,只需package.cmd
在那里運(yùn)行,打包后花費(fèi)一些時(shí)間,您將獲得dist/script.exe
.game.zip
有下一個(gè)文件: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 發(fā)行版的位置)。
完整更正和改進(jìn)的腳本代碼如下。您可能需要更改第一行 - 變量logname
等于寫入日志的路徑,如果程序崩潰,所有異常都會(huì)寫在那里,變量等于fontname
位于 中的字體的文件名,您可以將此字體替換為您喜歡的字體。./fonts/arial.ttf
game.zip
腳本可以通過python script.py
命令運(yùn)行,也可以通過將其打包到script.exe
using中來運(yùn)行pyinstaller
。
接下來進(jìn)行了改進(jìn):
添加
try
/except
全局塊是為了捕獲所有程序的異常/錯(cuò)誤,內(nèi)部except
塊錯(cuò)誤將保存到帶有logname
變量路徑的日志文件中。添加
faulthandler
到腳本的第一行,以捕獲所有嚴(yán)重錯(cuò)誤,例如Segmentation Fault
.在第一行中添加
os.chdir(...)
,將工作目錄更改為運(yùn)行打包腳本的目錄。因?yàn)槟J(rèn)情況下打包腳本的工作目錄不等于腳本所在的目錄。添加
import pgzrun
在第一行和pgzrun.go()
最后一行中,這使得腳本可以僅通過python script.py
而不是腳本運(yùn)行pgzrun script.py
常見的命令行來運(yùn)行pygame
。為了允許使用 pyinstaller 輕松打包,此改進(jìn)是必要的。fontname = fontname
為所有函數(shù)調(diào)用添加了參數(shù)screen.draw.text...(...)
,因?yàn)槿绻唇o出字體文件名,打包腳本將崩潰。
try:
logname = 'c:/temp/pgzrun.log'
fontname = 'arial.ttf'
import faulthandler
faulthandler.enable()
import os, sys
script_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
os.chdir(script_dir)
import pgzrun
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), fontname=fontname)
for box in answer_boxes:
screen.draw.filled_rect(box, "orange")
screen.draw.textbox(str(time_left), timer_box, color=('black'), fontname=fontname)
screen.draw.textbox(question[0], main_box, color=('black'), fontname=fontname)
index = 1
for box in answer_boxes:
screen.draw.textbox(question[index], box, color=('black'), fontname=fontname)
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)
pgzrun.go()
except:
import traceback
with open(logname, 'a', encoding = 'utf-8') as f:
f.write(''.join(traceback.format_exc()) + '\n')

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
我在這種情況下使用 auto-py-to-exe https://pypi.org/project/auto-py-to-exe/
我的系統(tǒng):Windows 8.1 上的 Python 2.7 人們告訴我,該 exe 在 Linux + Wine 上也能很好地工作
添加回答
舉報(bào)