我正在尝试将通知发送到通知中心 (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/