- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我仍在为我的数据库开发 GUI,现在我有一个不同的错误:
Traceback (most recent call last):
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 84, in <module>
ex = Ui_MainWindow()
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 16, in __init__
self.setupUi(self)
File "G:\Python\Database Kast Thuis\PyQt Test\MainMenu_ui.py", line 55, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'Ui_MainWindow' object has no attribute 'setCentralWidget'
我对 PyQt 一点都不了解,所以我不明白问题出在哪里。
代码是:(这是编辑后的版本,原始版本发布在下面)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Menu.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(387, 317)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Natan Samuel Geldorp.Remytop-PC/Documents/music-note-sound-256.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.ButtonRemove = QtWidgets.QPushButton(self.centralwidget)
self.ButtonRemove.setObjectName("ButtonRemove")
self.gridLayout.addWidget(self.ButtonRemove, 10, 0, 1, 1)
self.ButtonShowAll = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowAll.setObjectName("ButtonShowAll")
self.gridLayout.addWidget(self.ButtonShowAll, 7, 0, 1, 1)
self.ButtonShowSpecific = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowSpecific.setObjectName("ButtonShowSpecific")
self.gridLayout.addWidget(self.ButtonShowSpecific, 8, 0, 1, 1)
self.Text = QtWidgets.QLabel(self.centralwidget)
self.Text.setObjectName("Text")
self.gridLayout.addWidget(self.Text, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.ButtonAdd = QtWidgets.QPushButton(self.centralwidget)
self.ButtonAdd.setObjectName("ButtonAdd")
self.gridLayout.addWidget(self.ButtonAdd, 9, 0, 1, 1)
self.ButtonExit = QtWidgets.QPushButton(self.centralwidget)
self.ButtonExit.setObjectName("ButtonExit")
self.gridLayout.addWidget(self.ButtonExit, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 387, 21))
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", "Main Menu"))
self.ButtonRemove.setText(_translate("MainWindow", "Remove an album"))
self.ButtonShowAll.setText(_translate("MainWindow", "Show all registerd albums"))
self.ButtonShowSpecific.setText(_translate("MainWindow", "Show a specific album"))
self.Text.setText(_translate("MainWindow", "Welcome to the MusicDatabase Version 2.1-2017"))
self.label.setText(_translate("MainWindow", "Please select one of the options below."))
self.ButtonAdd.setText(_translate("MainWindow", "Add an album"))
self.ButtonExit.setText(_translate("MainWindow", "Exit"))
self.label_2.setText(_translate("MainWindow", "Authors: N.S. Geldorp & H. Nahumury"))
self.ButtonExit.clicked.connect(self.ButtonExit)
def ButtonExit(self):
exit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(app.exec_())
有人看到问题了吗?
-纳坦
发布原始转换:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_Menu.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(387, 317)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/Natan Samuel Geldorp.Remytop-PC/Documents/music-note-sound-256.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.ButtonRemove = QtWidgets.QPushButton(self.centralwidget)
self.ButtonRemove.setObjectName("ButtonRemove")
self.gridLayout.addWidget(self.ButtonRemove, 10, 0, 1, 1)
self.ButtonShowAll = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowAll.setObjectName("ButtonShowAll")
self.gridLayout.addWidget(self.ButtonShowAll, 7, 0, 1, 1)
self.ButtonShowSpecific = QtWidgets.QPushButton(self.centralwidget)
self.ButtonShowSpecific.setObjectName("ButtonShowSpecific")
self.gridLayout.addWidget(self.ButtonShowSpecific, 8, 0, 1, 1)
self.Text = QtWidgets.QLabel(self.centralwidget)
self.Text.setObjectName("Text")
self.gridLayout.addWidget(self.Text, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.ButtonAdd = QtWidgets.QPushButton(self.centralwidget)
self.ButtonAdd.setObjectName("ButtonAdd")
self.gridLayout.addWidget(self.ButtonAdd, 9, 0, 1, 1)
self.ButtonExit = QtWidgets.QPushButton(self.centralwidget)
self.ButtonExit.setObjectName("ButtonExit")
self.gridLayout.addWidget(self.ButtonExit, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 387, 21))
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", "Main Menu"))
self.ButtonRemove.setText(_translate("MainWindow", "Remove an album"))
self.ButtonShowAll.setText(_translate("MainWindow", "Show all registerd albums"))
self.ButtonShowSpecific.setText(_translate("MainWindow", "Show a specific album"))
self.Text.setText(_translate("MainWindow", "Welcome to the MusicDatabase Version 2.1-2017"))
self.label.setText(_translate("MainWindow", "Please select one of the options below."))
self.ButtonAdd.setText(_translate("MainWindow", "Add an album"))
self.ButtonExit.setText(_translate("MainWindow", "Exit"))
self.label_2.setText(_translate("MainWindow", "Authors: N.S. Geldorp & H. Nahumury"))
最佳答案
Qt Designer 用于生成 View 的设计,pyuic5 将该设计转换为python 代码,此元素必须与逻辑一起使用,另外建议不要修改它。
第一个解决方案:
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
第二种解决方案:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
ui = Ui_MainWindow()
ui.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
第三种解决方案:
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
关于python - 属性错误 : 'Ui_MainWindow' object has no attribute 'setCentralWidget' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43260595/
我有不同的文件(main.py和layout.py),我想从文件layout.py更改QMainWindow的窗口(我已经缩短了它应该更改窗口大小的示例)。 它在 main.py 中工作正常,我可以从
这是我的类主窗口: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { menu* v = new menu
对于编程类项目,我们需要开发一个应用程序,并且必须使用 PYQT5 作为 GUI。 我有一个基于 QMainWindow 的类。有几行代码我不太明白。下面是初始化: # Snippet 1 class
//clposter.h class CLPoster : public QMainWindow { Q_OBJECT public: CLPoster(); private slot
不确定我问这个问题是否正确,因为我是 C++ 和 Qt 的新手。但是,假设我有一个 QWidget 的子类: class childofqwidget : public QWidget 我可以将指向子
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { this->se
我正在尝试使用 QT Designer 创建 GUI。我已使用以下教程将我的 .ui 设计器文件转换为 .py 文件:http://pyqt.sourceforge.net/Docs/PyQt5/de
我仍在为我的数据库开发 GUI,现在我有一个不同的错误: Traceback (most recent call last): File "G:\Python\Database Kast Thui
我是一名优秀的程序员,十分优秀!