第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我無(wú)法將我的 py 文件打包為 exe 文件

我無(wú)法將我的 py 文件打包為 exe 文件

長(zhǎng)風(fēng)秋雁 2024-01-27 14:52:38
我對(duì)編程非常陌生,我決定使用 python 作為我的第一個(gè)編程語(yǔ)言!所以我通過 Pygame 創(chuàng)建了一個(gè)沒有附加文件的游戲,它只是一個(gè) python 文件。這只是一個(gè)問答游戲,我通過代碼本身創(chuàng)建了一個(gè) GUI。使用 cmd 運(yùn)行游戲時(shí),游戲運(yùn)行得很好,但是當(dāng)我使用 pyinstaller 將其轉(zhuǎn)換為可執(zhí)行文件并嘗試運(yùn)行它時(shí),它只會(huì)閃爍一個(gè)空窗口黑屏,然后關(guān)閉。我什至嘗試使用 cx freeze 。我嘗試了很多解決方案,但沒有一個(gè)有效。任何幫助,將不勝感激。我對(duì)編程很陌生,所以如果你能回答我的問題,你能用初學(xué)者的話解釋一下嗎哈哈哈。這是我的游戲的代碼:import random from random import randintWIDTH = 1280HEIGHT = 720def 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 + 1def game_over():    global question, time_left, scoredecreaserps    scoredecreaserps = 0    message = 'Game Over. You got %s questions correct' %str(numques)    question = [message, '-', '-', '-', '-', 5]    time_left = 0def 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 + 1def 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 - scoredecreaserpsmain_box = Rect(50, 40, 820, 240)timer_box = Rect(990, 40, 240, 240)
查看完整描述

3 回答

?
一只名叫tom的貓

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 文件。


查看完整回答
反對(duì) 回復(fù) 2024-01-27
?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

我已經(jīng)成功解決了您的任務(wù),請(qǐng)執(zhí)行后續(xù)步驟:

  1. 必要的模塊需要一次性安裝:

python -m pip install pyinstaller pgzero requests
  1. 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()"
  1. 文件內(nèi)部game.zip已經(jīng)有所需的一切,只需package.cmd在那里運(yùn)行,打包后花費(fèi)一些時(shí)間,您將獲得dist/script.exe.

  2. 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.ttfgame.zip

腳本可以通過python script.py命令運(yùn)行,也可以通過將其打包到script.exeusing中來運(yùn)行pyinstaller。

接下來進(jìn)行了改進(jìn):

  1. 添加try/except全局塊是為了捕獲所有程序的異常/錯(cuò)誤,內(nèi)部except塊錯(cuò)誤將保存到帶有logname變量路徑的日志文件中。

  2. 添加faulthandler到腳本的第一行,以捕獲所有嚴(yán)重錯(cuò)誤,例如Segmentation Fault.

  3. 在第一行中添加os.chdir(...),將工作目錄更改為運(yùn)行打包腳本的目錄。因?yàn)槟J(rèn)情況下打包腳本的工作目錄不等于腳本所在的目錄。

  4. 添加import pgzrun在第一行和pgzrun.go()最后一行中,這使得腳本可以僅通過python script.py而不是腳本運(yùn)行pgzrun script.py常見的命令行來運(yùn)行pygame。為了允許使用 pyinstaller 輕松打包,此改進(jìn)是必要的。

  5. 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')


查看完整回答
反對(duì) 回復(fù) 2024-01-27
?
一只甜甜圈

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 上也能很好地工作


查看完整回答
反對(duì) 回復(fù) 2024-01-27
  • 3 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)