gpt4 book ai didi

python - 如何在 py2app 中包含 NSUserNotificationCenter

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:12 24 4
gpt4 key购买 nike

我正在 mac osx 10.8.5 上用 python 2.7 制作一个应用我想显示通知次数,因此使用 NSUserNotificationCenter。在 Eclipse 上运行代码时会收到通知。但是,问题是当我使用 py2app 制作应用程序时,没有收到通知。此外,打开控制台和终止的默认错误页面即将到来。请建议一些方法,如何在 py2app 生成的 dist 中包含通知,以便它可以在任何其他机器上运行。我的 setup.py 是

from setuptools import setup

APP=['CC4Box.py']
DATA_FILES= [('',['config.cfg'])]
OPTIONS={'iconfile':'cc.icns','argv_emulation': True,'plist':{'CFBundleShortVersionString':'1.0'}}

setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app']
)

我的通知代码是:

def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
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)


def notificationBalloon(title,msg):
notify(title1, msg1,"", sound=False)

在 eclipse 上,通知如预期的那样到来,但是,在行中产生导入错误:

NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')

但在终端中,这些行运行得很好。

最佳答案

我的猜测是,.lookUpClass() 应该在运行时解析。因此你实际上并不想在你的 py2app 中包含那个类。除非你自己写了这个类。

您想要包含的是 objc 和相关库。当你调用 py2app 时,确保它在你的 virtualenv 中。如果 python -m pydoc objc 有效,那么 python setup.py py2app 也应该有效。

关于python - 如何在 py2app 中包含 NSUserNotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19678342/

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