我需要在MacOS中显示通知。只需使用python运行脚本即可显示通知。但每当我使用Pyinstaller打包脚本时,我都会收到以下错误。好像Pyinstaller在使用Mac通知时遇到了一些问题。我也尝试过使用pync (Python binding for terminal notifier)
。在所有情况下,只需运行应用程序python notify.py
即可,但使用Pyinstaller冻结脚本似乎不起作用。
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Python/2.7/site-packages/PyInstaller-2.1-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/Foundation/__init__.py", line 10, in <module>
from CoreFoundation import *
File "/Library/Python/2.7/site-packages/PyInstaller-2.1-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/Users/jacob/mac-notify/build/notify/out00-PYZ.pyz/CoreFoundation", line 19, in <module>
File "/Users/jacob/mac-notify/build/notify/out00-PYZ.pyz/objc._bridgesupport", line 121, in initFrameworkWrapper
File "build/bdist.macosx-10.9-intel/egg/pkg_resources.py", line 939, in resource_exists
File "build/bdist.macosx-10.9-intel/egg/pkg_resources.py", line 1392, in has_resource
File "build/bdist.macosx-10.9-intel/egg/pkg_resources.py", line 1447, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
logout
我使用以下Python脚本来显示通知
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)
答案 0 :(得分:1)
我有类似的问题,这就是我的工作方式。您不需要使用Foundation
来解决Pyinstaller的问题(不确定为什么会导致问题)并且不会在代码的其他地方使用。
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)
此时,它可能会引发一些错误或通知不会显示。 只需将CFBundleIdentifier
密钥添加到pyinstaller生成的info.plist
文件中。