NSAlert.runModal()失败,出现异常“[NSAlert runModal]只能从主线程调用。其他线程上的行为未定义。“

2024-09-25 02:36:28 发布

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

我正在使用Pyobjc和python3编写一个MACOS应用程序。我试图用“确定”和“取消”按钮显示警报。下面的代码在守护进程线程中(不幸的是)。runModal()函数正在引发以下异常

“[NSAlert runModal]只能从主线程调用。其他线程上的行为未定义。“

我的代码在下面

    from AppKit import NSAlert, NSInformationalAlertStyle, NSApp

    alert = NSAlert.alloc().init()
    alert.setMessageText_(text)
    alert.setInformativeText_(title)
    alert.setAlertStyle_(NSInformationalAlertStyle)
    for button in title_buttons:
        alert.addButtonWithTitle_(button)
    NSApp.activateIgnoringOtherApps_(True)
    return alert.runModal() # This line throws exception. 

提前谢谢


Tags: 代码应用程序titlebuttonpyobjcmacosalert线程