问题
I made a GUI file from pyqt designer,which i converted to .py file.but when i load that .py code in my IDE (Pycharm and sublime text),and i try to run it,it runs without errors,but the physical aspect of GUI isn't loaded,i tried a custom code from the internet,which worked great,GUI shows up when i run that code. i will give simpler code than the one i'm currently working on,as it seems all of the code generated from pyqt designer doesn't work at all for me regarding its physical aspect.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(170, 200, 91, 30))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(30, 40, 113, 30))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
self.pushButton.clicked.connect(self.lineEdit.clear)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
回答1:
You have 2 options:
1. Assuming you have used the following command:
pyuic5 your_filename.ui -o your_filename.py
# or
# pyuic5 your_filename.ui > your_filename.py
That command does not generate a window object or call the show method so the window is not displayed, you must use option -x
:
pyuic5 your_filename.ui -o your_filename.py -x
2. Add the code that invokes an object:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(170, 200, 91, 30))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(30, 40, 113, 30))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
self.pushButton.clicked.connect(self.lineEdit.clear)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(w)
w.show()
sys.exit(app.exec_())
来源:https://stackoverflow.com/questions/56202939/unable-to-build-gui-from-the-code-from-pyqt-designer