問題描述
所以,我正在嘗試為我擁有的 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"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!