以下是使用PyObjC界面显示桌面通知的简单代码。当我在没有wx.app.MainLoop()的情况下调用notify方法时,它工作正常,但是没有wx.app.MainLoop()。
import Foundation, objc
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
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)
if __name__ == '__main__':
app = wx.App()
notify("Notify Tittle", "Notify Subtitle", "Notify body")
app.MainLoop() # commenting this line shows the desktop notification