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

CandyGum picture CandyGum · Aug 4, 2017 · Viewed 43.9k times · Source

So, I'm trying to make a UI for a python code I have, but keep stumbling up on problems...

Right now, all the code does is make a window, with 2 texteditor boxes, and one button. When the button is pressed, it prints 'Test'.

I used QTDesigner to make the UI, then PyQt5 to convert it to python, and edited the code.

My code:

   # -*- 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_())

This is the error I get:

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

Even when I change class Ui_Widget(QtGui.QWidget): to class Ui_Widget(QtGui.QtWidgets): I get AttributeError: module 'PyQt5.QtGui' has no attribute 'QtWidgets'

Anyone who knows what the problem may be?

Cheers.

Answer

sascha picture sascha · Aug 4, 2017

Your error is from here:

Ui_Widget(QtGui.QWidget)

It basically tells you what the problem is.

It seems you are mixing some QT4 and QT5 here as your import is in QT5-style, but QtGui.QWidget looks like QT4-style.

Replace the line with:

Ui_Widget(QtWidgets.QWidget)

which should be compatible according to the docs

Remark: I don't know what you are really doing, but when you mention this: Even when I change class Ui_Widget(QtGui.QWidget): to class Ui_Widget(QtGui.QtWidgets): I get AttributeError: module 'PyQt5.QtGui' has no attribute 'QtWidgets' That's correct. You already imported QtWidgets, and not from PyQt5.QtGui. Just use Ui_Widget(QtWidgets) there.

In short: all these errors seem to be related to refactoring in regards to the modules between QT4 and QT5. The docs should help.