1. <i id='5oRZe'><tr id='5oRZe'><dt id='5oRZe'><q id='5oRZe'><span id='5oRZe'><b id='5oRZe'><form id='5oRZe'><ins id='5oRZe'></ins><ul id='5oRZe'></ul><sub id='5oRZe'></sub></form><legend id='5oRZe'></legend><bdo id='5oRZe'><pre id='5oRZe'><center id='5oRZe'></center></pre></bdo></b><th id='5oRZe'></th></span></q></dt></tr></i><div id='5oRZe'><tfoot id='5oRZe'></tfoot><dl id='5oRZe'><fieldset id='5oRZe'></fieldset></dl></div>

      <small id='5oRZe'></small><noframes id='5oRZe'>

      <tfoot id='5oRZe'></tfoot>
      • <bdo id='5oRZe'></bdo><ul id='5oRZe'></ul>
      <legend id='5oRZe'><style id='5oRZe'><dir id='5oRZe'><q id='5oRZe'></q></dir></style></legend>
    1. AttributeError:QDialog 对象没有属性 setCentralWidget

      时间:2023-08-05
            <i id='PrecQ'><tr id='PrecQ'><dt id='PrecQ'><q id='PrecQ'><span id='PrecQ'><b id='PrecQ'><form id='PrecQ'><ins id='PrecQ'></ins><ul id='PrecQ'></ul><sub id='PrecQ'></sub></form><legend id='PrecQ'></legend><bdo id='PrecQ'><pre id='PrecQ'><center id='PrecQ'></center></pre></bdo></b><th id='PrecQ'></th></span></q></dt></tr></i><div id='PrecQ'><tfoot id='PrecQ'></tfoot><dl id='PrecQ'><fieldset id='PrecQ'></fieldset></dl></div>
              <tbody id='PrecQ'></tbody>
              <bdo id='PrecQ'></bdo><ul id='PrecQ'></ul>

              • <tfoot id='PrecQ'></tfoot>

                <small id='PrecQ'></small><noframes id='PrecQ'>

                <legend id='PrecQ'><style id='PrecQ'><dir id='PrecQ'><q id='PrecQ'></q></dir></style></legend>

              • 本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 PyQt5 中创建 QWebEngineView 的打印预览? 下一篇:使用 QComboBox 选择显示的不同小部件集

                相关文章

                最新文章

              • <tfoot id='NeZ1e'></tfoot>
                <legend id='NeZ1e'><style id='NeZ1e'><dir id='NeZ1e'><q id='NeZ1e'></q></dir></style></legend>
                  <bdo id='NeZ1e'></bdo><ul id='NeZ1e'></ul>

                  <i id='NeZ1e'><tr id='NeZ1e'><dt id='NeZ1e'><q id='NeZ1e'><span id='NeZ1e'><b id='NeZ1e'><form id='NeZ1e'><ins id='NeZ1e'></ins><ul id='NeZ1e'></ul><sub id='NeZ1e'></sub></form><legend id='NeZ1e'></legend><bdo id='NeZ1e'><pre id='NeZ1e'><center id='NeZ1e'></center></pre></bdo></b><th id='NeZ1e'></th></span></q></dt></tr></i><div id='NeZ1e'><tfoot id='NeZ1e'></tfoot><dl id='NeZ1e'><fieldset id='NeZ1e'></fieldset></dl></div>
                  1. <small id='NeZ1e'></small><noframes id='NeZ1e'>