gpt4 book ai didi

python - NSUserNotificationCenter.defaultUserNotificationCenter() 在 python 中返回 None

转载 作者:太空狗 更新时间:2023-10-30 01:29:36 25 4
gpt4 key购买 nike

我正在尝试通过 python 连接到 Mountain Lion 通知中心。我已经安装了 pyobjc 并按照说明进行操作 herehere .另见: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)

当我用参数调用通知函数时,我得到一个属性错误:

AttributeError: 'NoneType' 对象没有属性 'scheduleNotification_'

我不明白为什么 NSUserNotificationCenter.defaultUserNotificationCenter() 返回一个 NoneType 对象。我无法在 Internet 或 SO 上查询有关此事的任何信息。

最佳答案

因此,根据 Ned 关于使用默认 python 的建议,这可以正常工作。当我在 32 位 enthought 发行版上安装 pyobjc 时,它也有效。在我看来,pyobjc 仅适用于 32 位 python 发行版(我无法确认这一点,但到目前为止情况似乎如此)。

(注意:当我发布问题时,我已经在 64 位 enthought 发行版上安装了 pyobjc)。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com