5

Tôi đang cố gắng kết nối với trung tâm thông báo Mountain Lion thông qua python. Tôi đã cài đặt pyobjc và đang làm theo hướng dẫn herehere. Xem thêm: Working with Mountain Lion's Notification Center using PyObjCNSUserNotificationCenter.defaultUserNotificationCenter() trả về Không có trong python

Dưới đây là mã của tôi:

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) 

Khi tôi gọi là thông báo chức năng với các đối số tôi nhận được một lỗi thuộc tính:

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

Tôi không hiểu tại sao NSUserNotificationCenter.defaultUserNotificationCenter() là trả về một đối tượng NoneType. Tôi đã không thể truy vấn bất cứ điều gì về vấn đề này trên internet hoặc SO.

+1

Bạn không thể sao chép điều này chỉ với ba dòng mã ('import objc; NSUserNotificationCenter = objc.lookUpClass ('NSUserNotificationCenter'); in NSUserNotificationCenter.defaultUserNotificationCenter()') thay vì toàn bộ hàm? –

+0

Ah! Tôi cho rằng tôi có thể có! – deepak

+0

FWIW, thử nghiệm ba dòng hoạt động cho tôi bằng cách sử dụng MacPorts hiện tại 'python27' (@ 2.7.3_1) và' py27-pyobjc' (@ 2.5.1_0): nó trả về một thể hiện của '_NSConcreteUserNotificationCenter'. –

Trả lời

2

Vì vậy, điều này hoạt động tốt bằng cách sử dụng lời khuyên của Ned về việc sử dụng python mặc định. Nó cũng hoạt động khi tôi cài đặt pyobjc trên phân phối enthought 32 bit. Có vẻ như với tôi rằng pyobjc chỉ hoạt động trên các bản phân phối python 32 bit (tôi không thể xác nhận điều này, nhưng cho đến nay điều này có vẻ là trường hợp).

(Lưu ý: Khi tôi đăng câu hỏi tôi đã cài đặt pyobjc trên bản phân phối enthought 64 bit).

+1

pyobjc hoạt động tốt với phân phối python 64 bit. –

+0

Tuy nhiên, một số khung công tác của Apple sẽ không hoạt động đúng cách trừ khi tệp nhị phân nằm trong một gói (ứng dụng hoặc plugin). Enthought có cài đặt một khung công tác hay chỉ là một bản phân phối đơn giản không? –

+0

Enthought cài đặt dưới dạng khung. – deepak

Các vấn đề liên quan