使用 py2app 时,有没有办法自定义显示的回溯对话框? (或者显示不同的 Cocoa 对话框?)

发布于 2024-08-14 13:29:21 字数 657 浏览 5 评论 0原文

有没有一种简单的方法可以更好地控制 py2app 回溯对话框,或者只是一种显示 GUI 消息的好方法?

如果我在 py2app 脚本中引发异常,我会看到一个对话框,内容如下:

MyAppName 错误

我的应用程序名称错误

执行主脚本期间发生意外错误

MyRaishedError:这是我在引发错误时可以控制的文本。

它有打开控制台终止按钮。我的脚本需要检查驱动器中是否有某个 DVD,如果没有,我想显示一个错误对话框并退出。我希望对它有更多的控制权,因为我只能更改一些文本而无法控制按钮。

我尝试通过 applescript 调用 osascript 来执行“显示对话框”,但它给了我这样的错误:0:19:执行错误:不允许用户交互。 (-1713)我不是特别喜欢这种方式,但如果这是我能做的一切......

我真的不想包括像 Cocoa Dialogs 或类似 PyObjC 项目的东西...脚本本身非常小,我看不到仅仅为了获得对话框而添加 10 倍的脚本内容。

Is there an easy way to get any more control over the py2app traceback dialogs, or just a nice way to display GUI messages?

If I raise an exception in my py2app script, I get a dialog that says something like this:

MyAppName Error

MyAppName Error

An unexpected error has occurred during execution of the main script

MyRaisedError: This is the text that I can control when I raise the error.

It has Open Console and Terminate buttons. My script needs to check if a certain DVD is in the drive, if it's not, I want to show an error dialog and quit. I would like to have more control over it than this, as I can only change some of the text and can't control the buttons.

I tried calling osascript to do 'display dialog' via applescript, but it gave me an error like this: 0:19: execution error: No user interaction allowed. (-1713) I don't particularly like this way of doing it, but if it's all I can do...

I would really prefer not to include a big project like Cocoa Dialogs or something like a PyObjC project... the script itself is very tiny and I can't see adding 10x the meat of my script just to get the dialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南月 2024-08-21 13:29:21

您可以通过 py-appscript 如果您的 python 站点库中还没有它,可以通过 easy_install 安装。此示例在 py2app 生成的应用程序内部运行:

#!/usr/bin/env python
from osax import *
import py2app

def doit():
    sa = OSAX()
    sa.display_dialog("Python says hello!",
            buttons=["Hi!", "Howdy!", "Duuuude!"],
            default_button=3)

if __name__ == '__main__':
    doit()

Instead of using osascript, you can call display dialog via py-appscript which, if you don't already have it in your python site-library, can be installed via easy_install. This example works inside of a py2app-generated app:

#!/usr/bin/env python
from osax import *
import py2app

def doit():
    sa = OSAX()
    sa.display_dialog("Python says hello!",
            buttons=["Hi!", "Howdy!", "Duuuude!"],
            default_button=3)

if __name__ == '__main__':
    doit()
夏夜暖风 2024-08-21 13:29:21

只需在引发异常的代码部分放置一个标准的 Python try/catch 块,然后使用 NSAlert 告诉用户他们需要将 DVD 放入

Just put a standard Python try/catch block around the section of code that throws the exception, then use NSAlert to tell your users they need to put the DVD in

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文