简体   繁体   English

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

[英]AttributeError: module 'PyQt5.QtGui' has no attribute 'QWidget'

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.现在,所有代码所做的就是创建一个带有 2 个文本编辑器框和一个按钮的窗口。 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.我用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.干杯。

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

In short: all these errors seem to be related to refactoring in regards to the modules between QT4 and QT5.简而言之:所有这些错误似乎都与 QT4 和 QT5 之间模块的重构有关。 The docs should help.文档应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于AttributeError,导入matplotlib.pyplot在PyCharm中失败:模块'PyQt5.QtGui'没有属性'QApplication' - Importing matplotlib.pyplot fails in PyCharm due to AttributeError: module 'PyQt5.QtGui' has no attribute 'QApplication' Python PyQt5 AttributeError:类型 object 'QWidget' 没有属性 'radioButton' - Python PyQt5 AttributeError: type object 'QWidget' has no attribute 'radioButton' PyQt QWidget析构函数错误“ AttributeError:'NoneType'对象没有属性'path'” - PyQt QWidget destructor error `AttributeError: 'NoneType' object has no attribute 'path'` ImportError:无法从“PyQt5.QtGui”导入名称“QColorConstants” - ImportError: cannot import name 'QColorConstants' from 'PyQt5.QtGui 属性错误模块“时间”没有属性“时钟”(pyqt5) - attributeerror module 'time' has no attribute 'clock' (pyqt5) 在QWidget中使用PyQt4.QtGui.QMouseEvent - Using PyQt4.QtGui.QMouseEvent in a QWidget AttributeError:“ PySide.QtGui.QPixmap”对象没有属性“ setDevicePixelRatio” - AttributeError: 'PySide.QtGui.QPixmap' object has no attribute 'setDevicePixelRatio' AttributeError: 模块 ' ' 没有属性 ' ' - AttributeError: module ' ' has no attribute ' ' AttributeError:模块“ ...”没有属性“ ...” - AttributeError: module '…' has no attribute '…' AttributeError:模块没有属性 - AttributeError: module has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM