兼容Mac的PyQt5错误

时间:2018-05-20 17:59:34

标签: python windows macos pyqt5 compatibility

我最近用PyQt5 UI编写了一个应用程序,该程序在Windows上运行得很好。但是,在Mac上运行时,UI会弹出,但不再是交互式的;按钮或输入字段都不起作用,每个元素都是灰色的。代码相同,似乎不包含任何特定于任一操作系统的内容。该程序是在Windows 10上编写并运行的,并且在Mac OSX El Capitan上遇到了麻烦。

如何解决此问题?

以下是一个例子。以下代码将在Windows上提供完全交互式UI,但在Mac上提供无法运行的“冻结”和灰色UI,即使在Mac上从头开始构建也是如此。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test2.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(403, 365)
        self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(19, 9, 361, 181))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.directory = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.directory.setObjectName("directory")
        self.gridLayout.addWidget(self.directory, 5, 3, 1, 1)
        self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 3, 0, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 2, 0, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 4, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 5, 0, 1, 1)
        self.filename = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.filename.setObjectName("filename")
        self.gridLayout.addWidget(self.filename, 4, 3, 1, 1)
        self.URL = QtWidgets.QLineEdit(self.verticalLayoutWidget)
        self.URL.setObjectName("URL")
        self.gridLayout.addWidget(self.URL, 3, 3, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 6, 0, 1, 1)
        self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 2, 3, 1, 1)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.checkBox_2 = QtWidgets.QCheckBox(self.verticalLayoutWidget)
        self.checkBox_2.setObjectName("checkBox_2")
        self.horizontalLayout_3.addWidget(self.checkBox_2)
        self.checkBox_3 = QtWidgets.QCheckBox(self.verticalLayoutWidget)
        self.checkBox_3.setObjectName("checkBox_3")
        self.horizontalLayout_3.addWidget(self.checkBox_3)
        self.checkBox = QtWidgets.QCheckBox(self.verticalLayoutWidget)
        self.checkBox.setObjectName("checkBox")
        self.horizontalLayout_3.addWidget(self.checkBox)
        self.gridLayout.addLayout(self.horizontalLayout_3, 6, 3, 1, 1)
        self.verticalLayout_2.addLayout(self.gridLayout)
        self.horizontalLayout.addLayout(self.verticalLayout_2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayoutWidget_3 = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(20, 190, 361, 153))
        self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.main_out = QtWidgets.QTextBrowser(self.verticalLayoutWidget_3)
        self.main_out.setObjectName("main_out")
        self.verticalLayout_3.addWidget(self.main_out)
        self.progress = QtWidgets.QProgressBar(self.verticalLayoutWidget_3)
        self.progress.setProperty("value", 24)
        self.progress.setObjectName("progress")
        self.verticalLayout_3.addWidget(self.progress)
        self.cancel_button = QtWidgets.QDialogButtonBox(self.verticalLayoutWidget_3)
        self.cancel_button.setOrientation(QtCore.Qt.Horizontal)
        self.cancel_button.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.cancel_button.setObjectName("cancel_button")
        self.verticalLayout_3.addWidget(self.cancel_button)

        self.retranslateUi(Dialog)
        self.cancel_button.accepted.connect(Dialog.accept)
        self.cancel_button.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label_3.setText(_translate("Dialog", "URL:"))
        self.label_2.setText(_translate("Dialog", "Save As:"))
        self.label.setText(_translate("Dialog", "Save In:"))
        self.label_4.setText(_translate("Dialog", "TextLabel"))
        self.checkBox_2.setText(_translate("Dialog", "CheckBox"))
        self.checkBox_3.setText(_translate("Dialog", "CheckBox"))
        self.checkBox.setText(_translate("Dialog", "CheckBox"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_Dialog()
    MainWindow = QtWidgets.QDialog()
    ui.setupUi(MainWindow)
    MainWindow.show()

0 个答案:

没有答案