这是使用 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