AttributeError:QDialog 对象没有属性 setCentralWidget

AttributeError: QDialog object has no attribute setCentralWidget(AttributeError:QDialog 对象没有属性 setCentralWidget)
本文介绍了AttributeError:QDialog 对象没有属性 setCentralWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 QT Designer 创建一个 GUI.我已使用以下教程将我的 .ui 设计器文件转换为 .py 文件:

例如,您选择了一个主窗口,因此您必须使用 QMainWindow 来实现它.

<代码>## 创建者:PyQt5 UI 代码生成器 5.7## 警告!在此文件中所做的所有更改都将丢失!导入系统从 PyQt5 导入 QtCore、QtGui、QtWidgets从 PyQt5.QtWidgets 导入 QApplication、QMainWindow类 Ui_ImageDialog(对象):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(160, 200, 191, 151))self.pushButton.setObjectName("pushButton")self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_2.setGeometry(QtCore.QRect(450, 200, 191, 151))self.pushButton_2.setObjectName("pushButton_2")self.label = QtWidgets.QLabel(self.centralwidget)self.label.setGeometry(QtCore.QRect(150, 70, 501, 101))调色板 = QtGui.QPalette()画笔 = QtGui.QBrush(QtGui.QColor(255, 0, 255))Brush.setStyle(QtCore.Qt.SolidPattern)调色板.setBrush(QtGui.QPalette.Active,QtGui.QPalette.WindowText,画笔)画笔 = QtGui.QBrush(QtGui.QColor(255, 0, 255))Brush.setStyle(QtCore.Qt.SolidPattern)调色板.setBrush(QtGui.QPalette.Inactive,QtGui.QPalette.WindowText,画笔)画笔 = QtGui.QBrush(QtGui.QColor(127, 127, 127))Brush.setStyle(QtCore.Qt.SolidPattern)调色板.setBrush(QtGui.QPalette.Disabled,QtGui.QPalette.WindowText,画笔)self.label.setPalette(调色板)字体 = QtGui.QFont()字体.setPointSize(72)self.label.setFont(字体)self.label.setLayoutDirection(QtCore.Qt.LeftToRight)self.label.setObjectName("标签")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))self.menubar.setObjectName("菜单栏")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("状态栏")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton.setText(_translate("MainWindow", "打开输入文件:"))self.pushButton_2.setText(_translate("MainWindow", "Run"))self.label.setText(_translate("MainWindow", "Happy Scraping"))应用程序 = QApplication(sys.argv)窗口 = QMainWindow()ui = Ui_ImageDialog()ui.setupUi(窗口)窗口.show()sys.exit(app.exec_())

如果你想实现一个 QDialog,你必须选择一些对话框(底部有按钮的对话框,右侧按钮的对话框或没有按钮的对话框).

I'm trying to create a GUI with QT Designer. I've converted my .ui designer file to a .py file using the following tutorial: http://pyqt.sourceforge.net/Docs/PyQt5/designer.html.

I have the following code:

#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog

class Ui_ImageDialog(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(160, 200, 191, 151))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(450, 200, 191, 151))
        self.pushButton_2.setObjectName("pushButton_2")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(150, 70, 501, 101))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.label.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(72)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        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", "Open Input File:"))
        self.pushButton_2.setText(_translate("MainWindow", "Run"))
        self.label.setText(_translate("MainWindow", "Happy Scraping"))

app = QApplication(sys.argv)
window = QDialog()

ui = Ui_ImageDialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

However, when I run this script, I get the following error:

File "/Users/tituskex/scrapeUI.py", line 42, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QDialog' object has no attribute 'setCentralWidget'

Q: Does anyone know how to fix this?

解决方案

When you use qt-designer you can choose the type of widget you want to design:

For example you have chosen a Main Window, therefore you must implement it with QMainWindow.

#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_ImageDialog(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(160, 200, 191, 151))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(450, 200, 191, 151))
        self.pushButton_2.setObjectName("pushButton_2")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(150, 70, 501, 101))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.label.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(72)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        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", "Open Input File:"))
        self.pushButton_2.setText(_translate("MainWindow", "Run"))
        self.label.setText(_translate("MainWindow", "Happy Scraping"))

app = QApplication(sys.argv)
window = QMainWindow()

ui = Ui_ImageDialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

If you want to implement a QDialog you must choose some of the Dialog(Dialog with Buttons Bottom, Dialog with Buttons Right or Dialog without Buttons).

这篇关于AttributeError:QDialog 对象没有属性 setCentralWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multiprocessing on Windows breaks(Windows 上的多处理中断)
How to use a generator as an iterable with Multiprocessing map function(如何将生成器用作具有多处理映射功能的可迭代对象)
read multiple files using multiprocessing(使用多处理读取多个文件)
Why does importing module in #39;__main__#39; not allow multiprocessig to use module?(为什么在__main__中导入模块不允许multiprocessig使用模块?)
Trouble using a lock with multiprocessing.Pool: pickling error(使用带有 multiprocessing.Pool 的锁时遇到问题:酸洗错误)
Python sharing a dictionary between parallel processes(Python 在并行进程之间共享字典)