NSUserNotificationCenter.defaultUserNotificationCenter()使用PyInstaller返回无 [英] NSUserNotificationCenter.defaultUserNotificationCenter() returning None using PyInstaller

查看:117
本文介绍了NSUserNotificationCenter.defaultUserNotificationCenter()使用PyInstaller返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将通知放到通知中心(Mac OSX) 我正在使用PyObjC绑定从我们的python应用程序中使用可可api.

I am trying to put notification to notification centre (Mac OSX) I am using PyObjC bindings to use cocoa api's from our python application.

我正在使用以下代码段:

I am using following code snippet :

import Foundation¬
import objc¬

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

notification = NSUserNotification.alloc().init()¬
notification.setTitle_("TestTitle")¬
notification.setInformativeText_("This is sample text")¬
center = NSUserNotificationCenter.defaultUserNotificationCenter()¬
center.deliverNotification_(notification)¬

当我直接从python上面运行时,它运行良好,并在通知中心显示通知.但是,当我使用PyInstaller打包上述程序以准备二进制文件并运行它时,出现以下错误.

When I run above directly from python it runs fine and shows notification in notification centre. However when I package above program using PyInstaller to prepare binary and run it gives following error.

AttributeError: 'NoneType' object has no attribute 'deliverNotification_'

这意味着我没有成为默认用户通知中心的对象.

This means I am not getting object of default user notification centre.

有人遇到过这个问题吗?

Has somebody come across this problem?

谢谢.

推荐答案

找到了答案.事实证明,我们需要在App的Info.plist文件中定义的"CFBundleIdentifier"键,默认情况下在PyInstaller生成的plist文件中不存在该键.否则,应用程序将无法访问通知中心.

Found an answer. Turns out we need 'CFBundleIdentifier' Key defined in App's Info.plist file which is not present by default in PyInstaller's generated plist file. Otherwise app does not get an access to Notification Center.

这篇关于NSUserNotificationCenter.defaultUserNotificationCenter()使用PyInstaller返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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