【问题标题】:Want to convert pygame project to windows executable file想将pygame项目转换为windows可执行文件
【发布时间】:2019-06-07 15:41:18
【问题描述】:

我使用名为“Python 速成课程”的书中的 pygame 创建了 Python 游戏。它有 9 个 .py 文件(1 个用于运行游戏,另外 8 个用于模块,例如:设置、game_functions 等)和一个文件夹,其中包含 2 个图像文件。想把这个项目分享给没有python和pygame的朋友,所以想把整个项目转成.exe(windows可执行文件)

我尝试过 cx_Freeze、py2exe 和 pyinstaller。 Pyinstaller 是最成功的一个,它创建了 .exe 文件,但是当我运行它时,它会出现黑屏并关闭。我认为原因是它无法访问我正在使用的模块(8 个 .py 文件)和/或图像文件。

这里是主要的 .py 代码,它导入了什么和东西..

import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button


def run_game():
        pygame.init()
...

如果您需要任何其他信息,请随时询问!

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    首先,我建议您在有代码要显示时使用Markdown;它真的很简单,并且使它更容易阅读。 例如,在代码前添加 ```python 并在末尾添加 ```。代码将被格式化并具有语法突出显示。例如,您的代码不会显示在一行上,而是:

    import pygame
    from pygame.sprite import Group
    from settings import Settings
    from ship import Ship
    import game_functions as gf
    from game_stats import GameStats
    from scoreboard import Scoreboard
    from button import Button
    
    def run_game(): pygame.init() ...
    

    现在,谈谈你的问题。根据cx_Freeze documentation

    不同的基础服务于 Windows 上不同类型的应用程序(GUI、控制台应用程序或服务)。

    使用 cx_Freeze,您需要在 setup.py 文件中指定应用程序是 GUI(图形用户界面),例如:

    import sys
    from cx_Freeze import setup, Executable
    
    # GUI applications require a different base on Windows (the default is for a
    # console application).
    base = None
    if sys.platform == "win32":
        base = "Win32GUI"
    
    setup(  name = "guifoo",
            version = "0.1",
            description = "My GUI application!",
            executables = [Executable("guifoo_main.py", base=base)])
    

    我还没有检查所有其他“python 到独立应用程序转换器”,但我想可能需要更改类似的内容才能使应用程序正确执行,而不是作为控制台应用程序。当出现此类问题时,建议您花时间仔细阅读它的文档。

    希望有帮助!

    【讨论】:

    • 感谢您的回复!这对我帮助很大。抱歉让这篇文章读起来不舒服,我是 stackoverflow 的新手,不知道 Markdown 函数。我唯一需要添加到您的代码中的是包含图像文件,因为 cx_Freeze 无法自行访问这些文件,因此包含带有 options={'build_exe': {'include_files': include_files}} 的文件,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2018-01-12
    • 2017-04-18
    相关资源
    最近更新 更多