我正在尝试通过python连接到Mountain Lion通知中心。我安装了pyobjc,并按照here和here的说明操作。另请参阅:Working with Mountain Lion's Notification Center using PyObjC
这是我的代码:
import Foundation, objc
import AppKit
import sys
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
""" Python method to show a desktop notification on Mountain Lion. Where:
title: Title of notification
subtitle: Subtitle of notification
info_text: Informative text of notification
delay: Delay (in seconds) before showing the notification
sound: Play the default notification sound
userInfo: a dictionary that can be used to handle clicks in your
app's applicationDidFinishLaunching:aNotification method
"""
notification = NSUserNotification.alloc().init()
notification.setTitle_(title)
notification.setSubtitle_(subtitle)
notification.setInformativeText_(info_text)
notification.setUserInfo_(userInfo)
if sound:
notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
当我用参数调用notify函数时,我得到一个属性错误:
AttributeError: 'NoneType' object has no attribute 'scheduleNotification_'
我不明白为什么NSUserNotificationCenter.defaultUserNotificationCenter()
返回一个NoneType对象。我无法在互联网或SO上查询此事。
答案 0 :(得分:2)
因此,使用Ned的默认python建议可以正常工作。当我在32位enthought发行版上安装pyobjc时,它也有效。在我看来,pyobjc仅适用于32位python发行版(我无法证实这一点,但到目前为止,似乎是这种情况)。
(注意:当我发布问题时,我已经在64位enthought发行版上安装了pyobjc)。