You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Python脚本构建的macOS应用在程序坞中表现正常?

问题描述

位于my.app/Contents/MacOS目录下的可执行文件是名为app.py的Python脚本,代码如下:

#!/usr/bin/python3
import os
from wsgiref.simple_server import make_server

with make_server(host=(host := '127.0.0.1'), port=(port := 1337),
                 app=lambda _, c: [c('200 OK', []), b'ok'][1:]) as s:
    os.system(f'open http://{host}:{port}')
    s.serve_forever()

启动该应用后,程序坞图标会长时间跳动,之后跳动停止却不显示运行指示器。此时应用实际仍在运行,但看起来像已停止的固定程序,且跳动停止前无法移动图标;若应用已固定在程序坞中,能看到活动指示器。

能否通过Python脚本与macOS系统交互,让该应用表现得像正常应用?比如使用macOS自带Python及tkinter等预装库。

若要自行创建该应用,还需my.app/Contents/Info.plist文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key><string>app.py</string>
</dict>
</plist>
解决方案

可以通过引入macOS自带的tkinter库创建一个隐藏窗口,让系统识别这是标准GUI应用,从而修复程序坞的异常表现,无需额外安装依赖。

修改后的app.py代码

#!/usr/bin/python3
import os
import tkinter as tk
from wsgiref.simple_server import make_server

# 创建隐藏的tkinter窗口,让系统识别为GUI应用
root = tk.Tk()
root.withdraw()  # 仅隐藏窗口,不销毁

# 启动WSGI服务器
server = make_server(host=(host := '127.0.0.1'), port=(port := 1337),
                     app=lambda _, c: [c('200 OK', []), b'ok'][1:])
os.system(f'open http://{host}:{port}')

# 用tkinter主循环维持应用状态,替代原serve_forever
try:
    root.mainloop()
except KeyboardInterrupt:
    server.shutdown()

完善Info.plist配置

添加必要的应用标识,让系统更准确识别应用类型:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>app.py</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string> <!-- 明确标识为应用程序 -->
    <key>LSUIElement</key>
    <false/> <!-- 声明不是后台应用,确保程序坞显示正常状态 -->
</dict>
</plist>

原理

macOS对命令行应用和GUI应用的程序坞处理逻辑不同:原脚本是纯命令行服务,系统不会触发正常的程序坞状态更新;而tkinter创建的窗口会让系统将其识别为标准GUI应用,从而正确显示运行指示器,图标跳动后也可正常移动。隐藏窗口不会影响后台服务的运行,只是为应用添加了GUI身份标识。

内容的提问来源于stack exchange,提问作者Andy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:18:14