如何将Tkinter编译为MacOS可执行文件?

4

我正在尝试将一个Tkinter应用程序编译为MacOS的可执行文件。我尝试使用py2apppyinstaller。我几乎成功了,使用py2app,但是它返回以下错误:

回溯

The Info.plist file must have a PyRuntimeLocations array containing string values for preferred Python runtime locations.  
These strings should be "otool -L" style mach ids; "@executable_stub" and "~" prefixes will be translated accordingly.

这是setup.py的样子:

from setuptools import setup

APP = ['main.py']
DATA_FILES = ['config.json']
OPTIONS = {
    'argv_emulation': True
}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

以下是目录结构:

-modules/---__init.py__
|        | 
|        -- gui_module.py
|        |
|        -- scraper_module.py
|        |
|        -- app.ico
|
-config.json
|
-countries_list.txt
|
-main.py
|
-requirements.txt
|
-setup.py

如果需要,我很乐意分享更多细节和文件。

1个回答

0
问题在于你需要为你的MacOS上拥有的Python框架提供可执行路径。因此,我修改了setup.py文件。

setup.py

from setuptools import setup

class CONFIG:
    VERSION = 'v1.0.1'
    platform = 'darwin-x86_64'
    executable_stub = '/opt/homebrew/Frameworks/Python.framework/Versions/3.10/lib/libpython3.10.dylib' # this is important, check where is your Python framework and get the `dylib`
    APP_NAME = f'your_app_{VERSION}_{platform}'
    APP = ['main.py']
    DATA_FILES = [
        'config.json', 
        'countries_list.txt', 
        ('modules', ['modules/app.ico']),
        # this modules are automatically added if you use __init__.py in your folder
        # ('modules', ['modules/scraper_module.py']),
        # ('modules', ['modules/gui_module.py']),
    ]

    OPTIONS = {
        'argv_emulation': False,
        'iconfile': 'modules/app.ico',
        'plist': {
            'CFBundleName': APP_NAME,
            'CFBundleDisplayName': APP_NAME,
            'CFBundleGetInfoString': APP_NAME,
            'CFBundleVersion': VERSION,
            'CFBundleShortVersionString': VERSION,
            'PyRuntimeLocations': [
                executable_stub,
                # also the executable can look like this:
                #'@executable_path/../Frameworks/libpython3.4m.dylib',
            ]
        }
    }

def main():
    setup(
        name=CONFIG.APP_NAME,
        app=CONFIG.APP,
        data_files=CONFIG.DATA_FILES,
        options={'py2app': CONFIG.OPTIONS},
        setup_requires=['py2app'],
        maintainer='foo bar',
        author_email='foo@domain.com',
    )

if __name__ == '__main__':
    main()

然后你需要运行 python3 setup.py py2app,现在你可以双击 your_app_{VERSION}_{platform}.app 运行应用程序了。

py2app 文档的建议:

  1. 确保不使用 -A 标志
  2. 当程序使用 GUI 工具包(如 Tkinter)时,不要使用 --argv-emulation py2app options docs

如果你能更新这个答案的话会很棒。我在这个配置中仍然遇到了一个关于dylib的问题。也找不到你设置的kvp的任何参考。 - Nae

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接