首页 > 解决方案 > QFileDialog 在 MacOS 上不打开对话窗口

问题描述

我正在 MacOS 上用 Python 编写 Qt GUI (PyQt5)。python test.py我在终端中运行我的程序。

当我尝试使用 QFileDialog 静态方法(例如 QFileDialog.getOpenFileName)时,我收到以下错误,而不是打开对话框窗口:

2021-10-11 12:06:48.411 python[49048:17688470] +[NSXPCSharedListener endpointForReply:withListenerName:]: an error occurred while attempting to obtain endpoint for listener 'com.apple.view-bridge': Connection interrupted

我可以通过不断重新启动应用程序并尝试打开对话框直到它工作来解决这个问题。它通常在 3 - 5 次重新启动后工作。这对于任何未来的用户或者如果在重新启动之前需要将数据保存在应用程序中,这并不理想。

这是一个最小的可重现示例脚本:

import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QPushButton,
    QFileDialog
    )


class MainWindow(QWidget):
    """The main window of the application"""

    def __init__(self, *args, **kwargs):
        """Initializes and shows the main window of the application"""

        super().__init__(*args, **kwargs)
        self.setLayout(QVBoxLayout())

        # open file button
        self.open_file_button = QPushButton(self)
        self.open_file_button.setText("Open File")
        self.open_file_button.clicked.connect(self.open_file)

        self.layout().addWidget(self.open_file_button)

        self.show()

    def open_file(self):
        file_name, _ = QFileDialog.getOpenFileName(self)

        if file_name:
            ...


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    sys.exit(app.exec())

macOS 版本:Catalina 10.15.1 (19B88)

Python版本:3.8.11

PyQt5 版本:5.15.4

PyQt5-Qt5 版本:5.15.2

标签: pythonmacospyqt5qfiledialog

解决方案


推荐阅读