PyQt5,弹窗,自定义输入对话框

环境:PyQt5 + pycharm + python3.5

在Qt Designer里新建一个Dialog的类型(不能是Main Window,只能有一个)

PyQt5,弹窗,自定义输入对话框_第1张图片

新建之后随意添加一些控件接收用户输入

PyQt5,弹窗,自定义输入对话框_第2张图片

之后保存,会把这个Dialog存成一个单独的.ui文件。前面一直在Main Window 里找这个部分找了半天,真傻。

使用PYUIC把.ui文件转换为.py文件后是这样

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 327)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(40, 40, 54, 12))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(40, 90, 54, 12))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(40, 150, 54, 12))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(40, 130, 54, 12))
        self.label_4.setText("")
        self.label_4.setObjectName("label_4")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(80, 40, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(80, 90, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_3.setGeometry(QtCore.QRect(80, 150, 113, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.toolButton = QtWidgets.QToolButton(Dialog)
        self.toolButton.setGeometry(QtCore.QRect(220, 90, 37, 18))
        self.toolButton.setObjectName("toolButton")
        self.toolButton_2 = QtWidgets.QToolButton(Dialog)
        self.toolButton_2.setGeometry(QtCore.QRect(220, 150, 37, 18))
        self.toolButton_2.setObjectName("toolButton_2")

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "用户信息输入"))
        self.label.setText(_translate("Dialog", "姓名"))
        self.label_2.setText(_translate("Dialog", "身高"))
        self.label_3.setText(_translate("Dialog", "体重"))
        self.toolButton.setText(_translate("Dialog", "..."))
        self.toolButton_2.setText(_translate("Dialog", "..."))

因为这个文件只是在MainWindow里调用,所以也就不添加 if __name__ =='__main__'了

之后回到主程序里面,添加一个Button,用这个按钮来触发弹窗。并import dialog.py

self.pushButton_2.clicked.connect(self.show_dialog)

类内写槽函数

    def show_dialog(self):
        self.di = QDialog()
        d = dialog.Ui_Dialog()
        d.setupUi(self.di)
        self.di.show()

Qt Designer 自动生成的类继承自Object,没有show方法,所以需要生成一个QDialog对象来重载我们自己又生成的Ui_Dialog

注意:是对QDialog对象show(),并不是自己生成的Ui_Dialog对象 show(),开始没有写self.di,弹窗总是一闪而过,类的的函数加上self之后成功。

和MainWindow的显示函数一个路子

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec())

 

你可能感兴趣的:(Python的小实践)