我正在用Python2.7在MacOSX10.8.5上做一个应用程序,我想要显示通知的次数,因此使用NSUserNotificationCenter。在eclipse上运行代码时会收到通知。但是,问题出在when I made app using py2app, Notifications are not coming。此外,打开控制台和终止错误的默认页面即将到来。请建议一些方法,如何在由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') 但在终端中,这些行运行得很好。
发布于 2013-11-18 21:57:50
我的猜测是,.lookUpClass()应该在运行时被解析。因此,您实际上并不想在py2app中包含该类。除非这个类是你自己写的。
您想要包含的是objc和相关的库。当你调用py2app时,确保它在你的虚拟环境中。如果python -m pydoc objc可以工作,python setup.py py2app也应该可以工作。
发布于 2013-11-19 11:46:24
如果您正在尝试创建一个弹出窗口来通知用户某些信息,有大量的python模块可用于此目的。Wx python是一个很好的选择。以下是弹出窗口的文档:
http://wxpython.org/docs/api/wx.PopupWindow-class.html
编辑:这不会以你想要的方式收到苹果的通知。试试这段代码。它使用一个名为terminal-notifier的可下载命令行工具来生成通知,通过python经由子进程访问:
import subprocess
def notification(title, subtitle, message):
subprocess.Popen(['terminal-notifier','-message',message,'-title',title,'-subtitle',subtitle])
notification(title = 'notification title', subtitle = 'subtitle', message = 'Hello World')有关更多信息,请访问此处,您可以在此处获取源代码和文档:
https://stackoverflow.com/questions/19678342
复制相似问题