【问题标题】:How to reference a file in the source code after adding it in data_files在data_files中添加文件后如何在源代码中引用文件
【发布时间】:2015-04-07 15:58:59
【问题描述】:

我刚开始尝试将我的 python 代码转换为 .app 文件来运行它。我成功地制作了 .app 文件,但是每当应该有图片时,我都会收到错误消息。它找不到文件或目录。因此,我将它们放在 setup.py 中的 data_files 中,如下所示:

from setuptools import setup
from glob import glob

DATA_FILES = [
    ('images', glob('GamePics/*.gif')),
]
setup(app=['Wellness Game.py'],
data_files=DATA_FILES,
setup_requires=["py2app"])

然后我使用新的 setup.py 重新制作了 .app 文件,但我仍然收到相同的错误消息。这就是我在源代码中引用图片的方式。

downstairspic=PhotoImage(file="../GamePics/DownStairs.gif")

请帮忙!我不明白为什么它不起作用!

【问题讨论】:

    标签: python user-interface python-3.x tkinter py2app


    【解决方案1】:

    您需要参考与您从中加载的 python 文件相关的文件。在你的情况下,一个简单的

    downstairspic=PhotoImage(file="os.path.join(
       os.path.dirname(__file__), ../GamePics/DownStairs.gif")
    )
    

    在每个 python-module 中(除了您启动的 ma​​in-module),file 应该指向该模块所在的绝对路径。 p>

    这里也有一些替代品 - setuptools 资源,而且你在 Mac 上也可以使用

    NSBundle.mainBundle().pathForResource_ofType_("DownStairs", "gif")
    

    但这意味着你需要 pyobjc。

    以下setup.py 为我工作:

    导入系统 从 setuptools 导入设置,find_packages 导入 glob,子进程 导入操作系统

    ICON = "Cyclops.icns"
    
    plist = {
        "CFBundleIconFile" : ICON,
        "CFBundleIdentifier" : "com.laseranimation.Cyclops",
        }
    
    
    execfile("cyclops/version.py")
    
    setup(
        name = "Cyclops",
        version = VERSION,
        packages = find_packages(),
        author = "Diez B. Roggisch",
        author_email = "deets@web.de",
        description = "A flexible Laser-Scanner to OSC converter with Filtering",
        license = "",
        keywords = "python pyobjc",
        app=["Cyclops.py"],
        data_files=["English.lproj", "Resources/" + ICON] + glob.glob("Resources/*qtz"),
        options=dict(py2app=dict(
            plist=plist,
        )),
    )
    

    【讨论】:

    • 嗯。我这样做了,但它仍然没有用。我认为问题可能出在 setup.py 中。我尝试检查应该是 GamePics 文件夹的 Resources 文件夹,但它不存在。
    • 可能是。我用适合我的 setup.py 扩充了我的答案。
    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2016-11-17
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多