Pyqt5:内置命令。AttributeError: 'QDialog'对象没有属性'setCentralWidget'

时间:2021-09-22 23:01:40

I converted ui file from Pyqt5 designer:

我从Pyqt5设计人员那里转换了ui文件:

Testing1.py

Testing1.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(380, 180, 112, 34))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Test1.py < is meant to add methods etc. etc.

Test1。py <表示添加方法等。< p>

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from testing1 import Ui_MainWindow

class myprog(Ui_MainWindow):
    def __init__ (self, dialog):
        Ui_MainWindow.__init__(self)
        self.setupUi(dialog)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    dialog = QtWidgets.QDialog()

    test1 = myprog(dialog)
    dialog.show()
    sys.exit(app.exec_())

If I run Testin1.py it's all ok, but if run second script, test1.py, i get following message:

如果我Testin1运行。py没有问题,但是如果运行第二个脚本test1。py,我收到如下信息:

> builtins.AttributeError: 'QDialog' object has no attribute
> 'setCentralWidget'

I am really confused what to do, I would really really appreciate, if you could help me solve this problem. Any help is more than welcome.

我真的不知道该怎么做,如果你能帮我解决这个问题,我会非常感激。任何帮助都是非常受欢迎的。

1 个解决方案

#1


2  

In Testing1.py file, you create a MainWindow. So in your Test1.py file, you should change the code

在Testing1。py文件,创建一个主窗口。所以你Test1中。py文件,您应该更改代码。

dialog = QtWidgets.QDialog()

to

dialog = QtWidgets.QMainWindow()

#1


2  

In Testing1.py file, you create a MainWindow. So in your Test1.py file, you should change the code

在Testing1。py文件,创建一个主窗口。所以你Test1中。py文件,您应该更改代码。

dialog = QtWidgets.QDialog()

to

dialog = QtWidgets.QMainWindow()