使用python在mac上简单的弹出气球消息

imp*_*imp 0 python macos user-interface popup

如何在mac上制作一个简单的弹出气球消息。我不想使用 NSUserNotification。使用 python-2.7 和 osx 10.8.5。POP-UP 不应该有任何按钮。弹出窗口应该来,显示消息并自动去。它也应该与 py2app 正确打包。

import objc
import Foundation
import AppKit

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) 
Run Code Online (Sandbox Code Playgroud)

Pet*_*aro 6

您可以display dialog在 AppleScript 中使用该语句并使用subprocess模块的call函数调用该脚本。

它可能看起来有点“ hackish的”,但因为你需要一个只对Mac的解决方案,我想这是最简单,最轻的解决方案,你可以得到,因为你没有当你使用任何类型的外部库或框架的收拾你项目成.app文件。

在此处输入图片说明

import subprocess

applescript = """
display dialog "Some message goes here..." ¬
with title "This is a pop-up window" ¬
with icon caution ¬
buttons {"OK"}
"""

subprocess.call("osascript -e '{}'".format(applescript), shell=True)
Run Code Online (Sandbox Code Playgroud)