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

2024-05-08 02:36:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在mac上制作一个简单的弹出气球消息。我不想使用NSUserNotification。 使用python-2.7和osx10.8.5。 弹出窗口不应该有任何按钮。弹出窗口应该会自动出现,显示消息并自动离开。它应该和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) 

Tags: textimportinfo消息titledefnotifynotification
1条回答
网友
1楼 · 发布于 2024-05-08 02:36:00

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

它可能看起来有点“黑客化”,但由于您需要一个仅限于Mac的解决方案,我想这是您可以得到的最简单、最轻的解决方案,因为当您将项目打包到.app文件中时,您不必使用任何外部库或框架。在

enter image description here

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)

相关问题 更多 >