python - NSUserNotificationCenter.defaultUserNotificationCenter() 使用 PyInstaller 返回 None

标签 python macos pyinstaller pyobjc

我正在尝试将通知发送到通知中心 (Mac OSX) 我正在使用 PyObjC 绑定(bind)来使用我们 python 应用程序中的 cocoa api。

我正在使用以下代码片段:

import Foundation¬
import objc¬

NSUserNotification = objc.lookUpClass('NSUserNotification')¬
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')¬

notification = NSUserNotification.alloc().init()¬
notification.setTitle_("TestTitle")¬
notification.setInformativeText_("This is sample text")¬
center = NSUserNotificationCenter.defaultUserNotificationCenter()¬
center.deliverNotification_(notification)¬

当我直接从 python 运行上面时,它运行良好并在通知中心显示通知。但是,当我使用 PyInstaller 打包上面的程序来准备二进制文件并运行它时,会出现以下错误。

AttributeError: 'NoneType' object has no attribute 'deliverNotification_'

这意味着我没有得到默认用户通知中心的对象。

有人遇到过这个问题吗?

提前致谢。

最佳答案

找到答案。事实证明,我们需要在 App 的 Info.plist 文件中定义的“CFBundleIdentifier”键,默认情况下,PyInstaller 生成的 plist 文件中不存在该键。否则应用无法访问通知中心。

关于python - NSUserNotificationCenter.defaultUserNotificationCenter() 使用 PyInstaller 返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25204797/

相关文章:

python - 数据框用空白替换下划线不起作用

ruby - 在没有输出的情况下获取不存在的系统用户信息

python - Python战舰

c# - 什么可能导致 GPIB 无响应

multithreading - 是否有任何理由将长间隔 NSTimer 移至后台线程?

objective-c - 使用 AVFoundation 调整输入音量(在 AVCaptureSession 中)

c# - 如何将我的 python 模块及其依赖项(例如 Numpy/Scipy)打包到一个文件中,例如用于 C# 调用的 dll?

python - 使用 PyInstaller 制作的 pyOpenGL exe 给出属性错误

python - 在其他计算机上测试 EXE 时找不到 PyInstaller ImportError DLL

python - 无法插入 Sqlite3 数据库