信号和插槽在另一个file.py中

时间:2019-02-24 05:50:15

标签: python-3.x pyqt pyqt5

在我使用QtDesigner创建的文件中,我将其称为f rm_main.py ,我不想在其中写入信号和插槽,这是因为每次我在qtDesigner中更新表单时,我有义务再次写信号和插槽。因此,我创建了另一个文件 frm_main_event.py 来创建信号和插槽。在 main.py 文件中,我只调用 frm_main_event.py 的Event()类,该类称为 frm_main.py

在frm_main_event.py中,我写了一个信号,它不起作用。您可以为我验证代码吗?

这是我使用QtDesigner创建的文件。我将其转换为.py

frm_main.py

    # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'user_interface/frm_main.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# 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(650, 424)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        self.stackedWidget.setObjectName("stackedWidget")
        self.page = QtWidgets.QWidget()
        self.page.setObjectName("page")
        self.gridLayout_4 = QtWidgets.QGridLayout(self.page)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.gridLayout_3 = QtWidgets.QGridLayout()
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.label = QtWidgets.QLabel(self.page)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.gridLayout_3.addWidget(self.label, 0, 0, 1, 1)
        self.gridLayout_4.addLayout(self.gridLayout_3, 0, 0, 1, 1)
        self.stackedWidget.addWidget(self.page)
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.gridLayout_6 = QtWidgets.QGridLayout(self.page_2)
        self.gridLayout_6.setObjectName("gridLayout_6")
        self.gridLayout_5 = QtWidgets.QGridLayout()
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.tabWidget = QtWidgets.QTabWidget(self.page_2)
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.gridLayout_7 = QtWidgets.QGridLayout(self.tab)
        self.gridLayout_7.setObjectName("gridLayout_7")
        self.formLayout = QtWidgets.QFormLayout()
        self.formLayout.setFieldGrowthPolicy(QtWidgets.QFormLayout.AllNonFixedFieldsGrow)
        self.formLayout.setObjectName("formLayout")
        self.firstNameLabel = QtWidgets.QLabel(self.tab)
        self.firstNameLabel.setObjectName("firstNameLabel")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.firstNameLabel)
        self.firstNameLineEdit = QtWidgets.QLineEdit(self.tab)
        self.firstNameLineEdit.setObjectName("firstNameLineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.firstNameLineEdit)
        self.lastNameLabel = QtWidgets.QLabel(self.tab)
        self.lastNameLabel.setObjectName("lastNameLabel")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.lastNameLabel)
        self.lastNameLineEdit = QtWidgets.QLineEdit(self.tab)
        self.lastNameLineEdit.setObjectName("lastNameLineEdit")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lastNameLineEdit)
        self.genderLabel = QtWidgets.QLabel(self.tab)
        self.genderLabel.setObjectName("genderLabel")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.genderLabel)
        self.genderComboBox = QtWidgets.QComboBox(self.tab)
        self.genderComboBox.setObjectName("genderComboBox")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.genderComboBox)
        self.typeLabel = QtWidgets.QLabel(self.tab)
        self.typeLabel.setObjectName("typeLabel")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.typeLabel)
        self.typeComboBox = QtWidgets.QComboBox(self.tab)
        self.typeComboBox.setObjectName("typeComboBox")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.typeComboBox)
        self.gridLayout_7.addLayout(self.formLayout, 0, 0, 1, 1)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem)
        self.btnValidateAddPer = QtWidgets.QPushButton(self.tab)
        self.btnValidateAddPer.setObjectName("btnValidateAddPer")
        self.horizontalLayout_3.addWidget(self.btnValidateAddPer)
        self.btnCancelAddPer = QtWidgets.QPushButton(self.tab)
        self.btnCancelAddPer.setObjectName("btnCancelAddPer")
        self.horizontalLayout_3.addWidget(self.btnCancelAddPer)
        self.gridLayout_7.addLayout(self.horizontalLayout_3, 1, 0, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_7.addItem(spacerItem1, 2, 0, 1, 1)
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        self.gridLayout_5.addWidget(self.tabWidget, 0, 0, 1, 1)
        self.gridLayout_6.addLayout(self.gridLayout_5, 0, 0, 1, 1)
        self.stackedWidget.addWidget(self.page_2)
        self.gridLayout.addWidget(self.stackedWidget, 0, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 650, 27))
        self.menubar.setObjectName("menubar")
        self.menuApplication = QtWidgets.QMenu(self.menubar)
        self.menuApplication.setObjectName("menuApplication")
        self.menuOperations = QtWidgets.QMenu(self.menubar)
        self.menuOperations.setObjectName("menuOperations")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setObjectName("actionClose")
        self.actionInscription = QtWidgets.QAction(MainWindow)
        self.actionInscription.setObjectName("actionInscription")
        self.actionSuppression = QtWidgets.QAction(MainWindow)
        self.actionSuppression.setObjectName("actionSuppression")
        self.actionSuppression_2 = QtWidgets.QAction(MainWindow)
        self.actionSuppression_2.setObjectName("actionSuppression_2")
        self.menuApplication.addAction(self.actionClose)
        self.menuOperations.addAction(self.actionInscription)
        self.menuOperations.addSeparator()
        self.menuOperations.addAction(self.actionSuppression_2)
        self.menubar.addAction(self.menuApplication.menuAction())
        self.menubar.addAction(self.menuOperations.menuAction())

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(1)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "Welcome to the app for school"))
        self.firstNameLabel.setText(_translate("MainWindow", "First name"))
        self.lastNameLabel.setText(_translate("MainWindow", "Last name"))
        self.genderLabel.setText(_translate("MainWindow", "Gender"))
        self.typeLabel.setText(_translate("MainWindow", "Type"))
        self.btnValidateAddPer.setText(_translate("MainWindow", "Validate transact"))
        self.btnCancelAddPer.setText(_translate("MainWindow", "Cancel"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Add Person"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Add Type of person"))
        self.menuApplication.setTitle(_translate("MainWindow", "Application"))
        self.menuOperations.setTitle(_translate("MainWindow", "Operations"))
        self.actionClose.setText(_translate("MainWindow", "Close"))
        self.actionInscription.setText(_translate("MainWindow", "Inscription"))
        self.actionSuppression.setText(_translate("MainWindow", "Suppression"))
        self.actionSuppression_2.setText(_translate("MainWindow", "Suppression"))

frm_main_event.py

import sys
from PyQt5 import QtCore
from qtpy import QtWidgets
from user_interface.frm_main import Ui_MainWindow


class Event(object):
    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.frm = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.frm)
        self.frm.show()
        sys.exit(self.app.exec_())

        QtCore.QObject.connect(self.ui.btnCancelAddPer, QtCore.SIGNAL("clicked()"), self.close_app)
        # self.ui.btnCancelAddPer.clicked.connect(self.close_app)
        # self.btnCancelAddPer.clicked.connect(self.close_app)
        self.ui.actionClose.triggered.connect(self.close_app)

    def close_app(self):
        self.ui.firstNameLineEdit.setText('yeah')

main.py

from user_interface.frm_main_event import Event

if __name__ == '__main__':
    frm = Event()

0 个答案:

没有答案