In the process of learning Qt, I encountered an error AttributeError: ‘Ui_MainWindow’ object has no attribute ‘setupUI’
code show as below:
# This Python file uses the following encoding: utf-8
import sys
from windowUI import Ui_MainWindow
from PySide2.QtWidgets import Application, QMainWindow
if __name__ == "__main__":
app = Application([])
aw = Ui_Mainwindow()
window = OMainwindow()
aw.setupUI(window)
window. show()
sys.exit(app.exec_())
The following code is the .py file generated by .ui
class Ui_MainWindow(object):
def setupui(self, Mainwindow):
Mainwindow.setobjectName("MainWindow")
Mainwince.resize(800, 600)
MainWinauw.setLayoutDirection(QtCore.Qt.RightToLeft)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setobjectName("centralwidget")
self.commandLinkButton = Qtwidgets.QCommandLinkButton(self. centralwidget)
self.commandLinkButton.setGeometry(QtCore.QRect(330, 230, 187, 41))
self.commandLinkButton.set0bjectName("commandLinkButton")
MainWindow.setCentralwidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(@tCore.QRect (0, 0, 800, 27))
self.menubar.set0bjectName("menubar")
Mainwindow.setMenuBar(self. menubar)
self.statusbar = Qtwidgets.QStatusBar(MainWindow)
self.statusbar.setobjectName("statusbar")
Mainwindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetalbject.connectSlotsByName(MainWindow)
def retranslateli(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
Mainwindow.setwindowTitle(_translate("MainWindow",
"MainWindow"))
self.commandLinkButton.setText(_translate("MainWindow","test"))
>Solution :
You made a mistake due to carelessness, The function defined in Ui_MainWindow is setupUi, and setupUI is used when calling, one is i, the other is I, the names are different, so an error is reported, and the function name can be changed to the same.