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

时间:2023-03-12
本文介绍了AttributeError:模块“PyQt5.QtGui"没有属性“QWidget"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

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

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

我使用 QTDesigner 制作 UI,然后使用 PyQt5 将其转换为 python,并编辑了代码.

I used QTDesigner to make the UI, then PyQt5 to convert it to python, and edited the 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_())

这是我得到的错误:

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'

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?

干杯.

推荐答案

你的错误来自这里:

Ui_Widget(QtGui.QWidget)

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

It basically tells you what the problem is.

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

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

将行替换为:

Ui_Widget(QtWidgets.QWidget)

应该根据docs

备注:我不知道你到底在做什么,但是当你提到这个时:即使我将 class Ui_Widget(QtGui.QWidget): 更改为 class Ui_Widget(QtGui.QtWidgets): 我得到 AttributeError:模块 'PyQt5.QtGui' 没有属性 'QtWidgets' 没错.您已经导入了 QtWidgets,而不是从 PyQt5.QtGui.只需在那里使用 Ui_Widget(QtWidgets).

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.

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

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

这篇关于AttributeError:模块“PyQt5.QtGui"没有属性“QWidget"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:捕获 QApplication 中引发的异常 下一篇:如何在没有元类冲突的情况下将泛型类型与 PyQt 子类一起使用?

相关文章