Python AttributeError: 模块“PyQt5.QtGui”没有属性“QWidget”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45501514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:05:21  来源:igfitidea点击:

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

pythonpyqt5qt-designer

提问by CandyGum

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

所以,我正在尝试为我拥有的 python 代码制作一个 UI,但不断遇到问题......

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'.

现在,所有代码所做的就是创建一个带有 2 个文本编辑器框和一个按钮的窗口。当按下按钮时,它会打印“测试”。

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

我用QTDesigner做UI,然后用PyQt5转python,编辑代码。

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'

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

Anyone who knows what the problem may be?

有谁知道可能是什么问题?

Cheers.

干杯。

回答by sascha

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.

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

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.

备注:我不知道你真正在做什么,但是当你提到这一点时: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'那是正确的。您已经导入了 QtWidgets,而不是从 PyQt5.QtGui。只需在那里使用 Ui_Widget(QtWidgets) 即可。

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

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