AttributeError:模块'PyQt5.QtGui'没有属性'QWidget'

时间:2017-08-04 08:11:23

标签: python pyqt5 qt-designer

所以,我正在尝试为我拥有的python代码创建一个UI,但不断绊倒问题......

现在,所有代码都是创建一个窗口,有2个texteditor框和一个按钮。按下按钮时,会打印“测试”。

我使用QTDesigner创建UI,然后使用PyQt5将其转换为python,并编辑代码。

我的代码:

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Widget(QtGui.QWidget):
    def setupUi(self, Widget):
        Widget.setObjectName("Widget")
        Widget.resize(681, 382)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Widget)
        self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.in_box = QtWidgets.QTextEdit(Widget)
        self.in_box.setMaximumSize(QtCore.QSize(200, 350))
        self.in_box.setObjectName("in_box")
        self.horizontalLayout.addWidget(self.in_box)
        self.convert = QtWidgets.QPushButton(Widget)
        self.convert.setObjectName("convert")
        self.horizontalLayout.addWidget(self.convert)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setContentsMargins(11, 11, 11, 11)
        self.gridLayout.setSpacing(6)
        self.gridLayout.setObjectName("gridLayout")
        self.out_box = QtWidgets.QTextEdit(Widget)
        self.out_box.setMaximumSize(QtCore.QSize(200, 350))
        self.out_box.setObjectName("out_box")
        self.gridLayout.addWidget(self.out_box, 6, 0, 1, 1)
        self.horizontalLayout.addLayout(self.gridLayout)

        self.retranslateUi(Widget)
        QtCore.QMetaObject.connectSlotsByName(Widget)

    def retranslateUi(self, Widget):
        _translate = QtCore.QCoreApplication.translate
        Widget.setWindowTitle(_translate("Widget", "Converter"))
        self.convert.setText(_translate("Widget", "Convert"))
    self.convert.clicked.connect(self.convertclick)

    def convertclick(self):
        print ('Test')



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Widget = QtWidgets.QWidget()
    ui = Ui_Widget()
    ui.setupUi(Widget)
    Widget.show()
    sys.exit(app.exec_())

这是我得到的错误:

AttributeError: module 'PyQt5.QtGui' has no attribute 'QWidget'

即使我改变class Ui_Widget(QtGui.QWidget):class Ui_Widget(QtGui.QtWidgets): 我得到AttributeError: module 'PyQt5.QtGui' has no attribute 'QtWidgets'

任何知道问题可能是什么的人?

干杯。

1 个答案:

答案 0 :(得分:6)

您的错误来自此处:

fread()

它基本上告诉你问题是什么。

看来你正在混合一些QT4和QT5,因为你的导入是QT5风格,但QtGui.QWidget看起来像QT4风格。

将该行替换为:

fwrite()

应根据docs

兼容

备注:我不知道你在做什么,但当你提到这一点时:#include <stdio.h> #define BUFFERSIZE 8192 // 8 kiB int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: %s source dest\n", argv[0]); return 1; } FILE *src = fopen(argv[1], "rb"); if (!src) { fprintf(stderr, "Error opening %s for reading.\n", argv[1]); return 1; } FILE *dst = fopen(argv[2], "wb"); if (!dst) { fclose(src); fprintf(stderr, "Error opening %s for writing.\n", argv[2]); return 1; } char buf[BUFFERSIZE]; int rc = 1; // <- failure do { size_t nread = fread(buf, 1, BUFFERSIZE, src); if (nread < BUFFERSIZE) { if (ferror(src)) { fprintf(stderr, "Error reading from %s.\n", argv[1]); goto done; } if (!nread) break; } if (fwrite(buf, 1, nread, dst) < nread) { fprintf(stderr, "Error writing to %s.\n", argv[2]); goto done; } } while (!feof(src)); rc = 0; // <- success done: fclose(dst); fclose(src); return rc; } 这是对的。您已导入QtWidgets,而不是PyQt5.QtGui。只需在那里使用Ui_Widget(QtWidgets)。

简而言之:所有这些错误似乎都与QT4和QT5之间模块的重构有关。文档应该有所帮助。