我想用QT設計器在PyCharm上測試我的第一個Python GUI。我有我需要的一切,我搜索了QT designer help site。在Pycharm上運行.ui文件
我做錯了什麼,但我不知道是什麼。
這是我的代碼:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(687, 221)
font = QtGui.QFont()
font.setPointSize(28)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayout_2 = QtWidgets.QFormLayout(self.centralwidget)
self.formLayout_2.setObjectName("formLayout_2")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.pushButton)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(14)
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.comboBox)
self.label = QtWidgets.QLabel(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setBold(False)
font.setUnderline(True)
font.setWeight(50)
font.setStrikeOut(False)
font.setKerning(True)
self.label.setFont(font)
self.label.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.label)
self.formLayout_2.setLayout(0, QtWidgets.QFormLayout.SpanningRole, self.formLayout)
MainWindow.setCentralWidget(self.centralwidget)
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", "Aceptar"))
self.comboBox.setItemText(0, _translate("MainWindow", "Facturacion"))
self.comboBox.setItemText(1, _translate("MainWindow", "Clientes"))
self.comboBox.setItemText(2, _translate("MainWindow", "Inventario"))
self.comboBox.setItemText(3, _translate("MainWindow", "Proveedores"))
self.comboBox.setItemText(4, _translate("MainWindow", "Log de Usuarios"))
self.label.setText(_translate("MainWindow", "Reportes"))
我試圖打開這個圖形用戶界面:
import sys
from PyQt5.QtWidgets import QApplication, QDialog
from Principal import Ui_MainWindow
app = QApplication(sys.argv)
window = QDialog()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
以下是錯誤:基於
Traceback (most recent call last):
File "C:/Users/arild/PycharmProjects/Reportes/Reportes.py", line 8, in <module>
ui.setupUi(window)
File "C:\Users\arild\PycharmProjects\Reportes\Principal.py", line 57, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QDialog' object has no attribute 'setCentralWidget'
究竟發生了什麼?你期望發生什麼? [請閱讀此]。(http://stackoverflow.com/help/mcve) –
我期待一個窗口彈出像QT設計器預覽。 唯一發生的是CDM窗口彈出半秒鐘然後關閉。 –
實際上*發生了什麼?任何錯誤?任何事情?你確定它正在執行嗎?你怎麼確定?您的系統的響應對於診斷正在發生的事情非常重要。 –