如何在py2app中包含NSUserNotificationCenter [英] how to include NSUserNotificationCenter in py2app

查看:125
本文介绍了如何在py2app中包含NSUserNotificationCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac osx 10.8.5上的python 2.7中制作一个应用,我想显示通知次数,因此使用NSUserNotificationCenter.在eclipse上运行代码时,通知即将到来.但是,问题是when I made app using py2app, Notifications are not coming.而且,打开控制台和终止的错误的默认页面即将到来.请提出一些方法,如何将Not2包含在py2app生成的dist中,以便它可以在任何其他机器上使用. 我的setup.py是

I am making an app in python 2.7 on mac osx 10.8.5 I want to show notification number of times, therefore using NSUserNotificationCenter. Notifications are coming while running code on eclipse. But, the issue is when I made app using py2app, Notifications are not coming. Moreover, the default page of error of open console and Terminate is coming. Please suggest some way, how to include Notification in dist generated by py2app, so that It will work on any other machine. My setup.py is

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上,通知如期到达,但是,在行中产生导入错误:

On eclipse, notifications are coming as expected, however, import error produced in lines:

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

但是在终端中,这些行运行良好.

but in terminal these lines are nicely run.

推荐答案

如果您试图创建一个弹出窗口以将某些信息通知用户,那么有很多python模块可用于此目的. Wx python是一个不错的选择.这是弹出窗口的文档:

If you are trying to create a pop-up window to notify the user of certain information, there are plenty of python modules for this purpose. Wx python is a good choice. Here is the documentation for pop-up windows:

http://wxpython.org/docs/api/wx.PopupWindow -class.html

不会以您想要的方式收到苹果通知.试试这个代码.它使用一个名为terminal-notifier的可下载命令行工具进行通知,该通知可通过子进程通过python访问:

That won't get an apple notification in the way you want. Try this code. It uses a downloadable command line tool called terminal-notifier to make notifications, accessed through python via sub process:

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')

这应该获得所需的结果,尽管要自动安装它,您需要在ruby中运行构建.您还可以让它播放声音,更改一些ID参数,甚至在您单击它时告诉它运行shell命令.有关更多信息,请点击此处,在这里您可以获取源代码和文档:

This should get the results you want, although to install it automatically you need to run a build in ruby. You could also get it to play sounds, change some ID parameters, and even tell it to run a shell command when you click on it. For more information go here, this is where you can get the source and the docs:

https://github.com/julienXX/terminal-notifier

这篇关于如何在py2app中包含NSUserNotificationCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆