问题描述
我正在尝试通过 python 连接到 Mountain Lion 通知中心.我已经安装了 pyobjc 并按照 here 和 这里.另请参阅:使用 PyObjC 使用 Mountain Lion 的通知中心
I am trying to connect to the Mountain Lion notification center via python. I've installed pyobjc and am following the instructions here and here. Also see: 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)
当我使用参数调用通知函数时,我收到一个属性错误:
When I call the notify function with arguments I get an attribute error:
AttributeError: 'NoneType' 对象没有属性 'scheduleNotification_'
我不明白为什么 NSUserNotificationCenter.defaultUserNotificationCenter()
返回一个 NoneType 对象.我无法在互联网或 SO 上查询有关此事的任何信息.
I don't understand why NSUserNotificationCenter.defaultUserNotificationCenter()
is returning a NoneType object. I've not been able to query anything on this matter on the internet or SO.
推荐答案
因此,使用 Ned 的使用默认 Python 的建议可以很好地工作.当我在 32 位 enthought 发行版上安装 pyobjc 时,它也有效.在我看来 pyobjc 仅适用于 32 位 python 发行版(我无法确认这一点,但到目前为止似乎确实如此).
So, this works fine using Ned's advice of using default python. It also worked when I installed pyobjc on the 32-bit enthought distribution. It seems to me that pyobjc works only on 32 bit python distributions (I cannot confirm this, but so far this seems to be the case).
(注意:当我发布问题时,我已经在 64 位 enthought 发行版上安装了 pyobjc).
(Note: When I posted the question I had installed pyobjc on the 64 bit enthought distribution).
这篇关于NSUserNotificationCenter.defaultUserNotificationCenter() 在 python 中返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!