如何在pyqt5中的按钮上编写python函数?

时间:2019-03-17 06:20:37

标签: python pyqt

我是python和pyqt的新手。我已经在pyqt5上制作了GUI,现在我想将此GUI连接到我已经编写的(.py)文件中的代码。如何将此python文件连接到我的GUI。

这是我的gui.py文件的代码

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1038, 942)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setStyleSheet("")
        self.tabWidget.setObjectName("tabWidget")
        self.tabTrain = QtWidgets.QWidget()
        self.tabTrain.setObjectName("tabTrain")
        self.line_edit_file_path = QtWidgets.QLineEdit(self.tabTrain)
        self.line_edit_file_path.setGeometry(QtCore.QRect(130, 70, 401, 41))
        self.line_edit_file_path.setObjectName("line_edit_file_path")
        self.btnBrowseDatasetFile = QtWidgets.QPushButton(self.tabTrain)
        self.btnBrowseDatasetFile.setGeometry(QtCore.QRect(550, 70, 191, 41))
        self.btnBrowseDatasetFile.setStyleSheet("font: 75 10pt \"Yu Gothic UI Semibold\";\n"
"")
        self.btnBrowseDatasetFile.setObjectName("btnBrowseDatasetFile")
        self.btn_start_training = QtWidgets.QPushButton(self.tabTrain)
        self.btn_start_training.setGeometry(QtCore.QRect(750, 70, 171, 41))
        self.btn_start_training.setStyleSheet("font: 75 10pt \"Yu Gothic UI Semibold\";")
        self.btn_start_training.setObjectName("btn_start_training")
        self.tableViewResults = QtWidgets.QTableView(self.tabTrain)
        self.tableViewResults.setGeometry(QtCore.QRect(130, 180, 801, 211))
        self.tableViewResults.setObjectName("tableViewResults")
        self.label = QtWidgets.QLabel(self.tabTrain)
        self.label.setGeometry(QtCore.QRect(130, 130, 211, 41))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Light")
        font.setPointSize(12)
        font.setBold(False)
        font.setUnderline(True)
        font.setWeight(50)
        self.label.setFont(font)
        self.label.setStyleSheet("\n"
"")
        self.label.setObjectName("label")
        self.label_graph_view = QtWidgets.QLabel(self.tabTrain)
        self.label_graph_view.setGeometry(QtCore.QRect(130, 460, 801, 371))
        self.label_graph_view.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.label_graph_view.setAlignment(QtCore.Qt.AlignCenter)
        self.label_graph_view.setObjectName("label_graph_view")
        self.label_3 = QtWidgets.QLabel(self.tabTrain)
        self.label_3.setGeometry(QtCore.QRect(130, 410, 211, 41))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Light")
        font.setPointSize(12)
        font.setBold(False)
        font.setUnderline(True)
        font.setWeight(50)
        self.label_3.setFont(font)
        self.label_3.setStyleSheet("")
        self.label_3.setObjectName("label_3")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.tabTrain)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 460, 95, 151))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.btn_naive_bayes = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_naive_bayes.setObjectName("btn_naive_bayes")
        self.verticalLayout.addWidget(self.btn_naive_bayes)
        self.btn_svm = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_svm.setObjectName("btn_svm")
        self.verticalLayout.addWidget(self.btn_svm)
        self.btn_logistic_regression = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_logistic_regression.setObjectName("btn_logistic_regression")
        self.verticalLayout.addWidget(self.btn_logistic_regression)
        self.btn_mlp = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.btn_mlp.setObjectName("btn_mlp")
        self.verticalLayout.addWidget(self.btn_mlp)
        self.label_2 = QtWidgets.QLabel(self.tabTrain)
        self.label_2.setGeometry(QtCore.QRect(210, 10, 621, 31))
        font = QtGui.QFont()
        font.setFamily("Nirmala UI")
        font.setPointSize(20)
        font.setUnderline(True)
        self.label_2.setFont(font)
        self.label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.tabWidget.addTab(self.tabTrain, "")
        self.tabTest = QtWidgets.QWidget()
        self.tabTest.setObjectName("tabTest")
        self.line_edit_file_path_2 = QtWidgets.QLineEdit(self.tabTest)
        self.line_edit_file_path_2.setGeometry(QtCore.QRect(150, 60, 401, 41))
        self.line_edit_file_path_2.setObjectName("line_edit_file_path_2")
        self.btn_search_query = QtWidgets.QPushButton(self.tabTest)
        self.btn_search_query.setGeometry(QtCore.QRect(560, 60, 191, 41))
        self.btn_search_query.setStyleSheet("font: 75 10pt \"Yu Gothic UI Semibold\";\n"
"")
        self.btn_search_query.setObjectName("btn_search_query")
        self.label_4 = QtWidgets.QLabel(self.tabTest)
        self.label_4.setGeometry(QtCore.QRect(150, 140, 161, 51))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.label_query_result = QtWidgets.QLabel(self.tabTest)
        self.label_query_result.setGeometry(QtCore.QRect(330, 150, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_query_result.setFont(font)
        self.label_query_result.setStyleSheet("color: rgb(0, 255, 0);\n"
"color: rgb(255, 0, 0);")
        self.label_query_result.setObjectName("label_query_result")
        self.tabWidget.addTab(self.tabTest, "")
        self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1038, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.line_edit_file_path.setPlaceholderText(_translate("MainWindow", "Path of Training File....."))
        self.btnBrowseDatasetFile.setText(_translate("MainWindow", "Browse Training File"))
        self.btn_start_training.setText(_translate("MainWindow", "Start Training"))
        self.label.setText(_translate("MainWindow", "Results"))
        self.label_graph_view.setText(_translate("MainWindow", "Graph of Classifier Training"))
        self.label_3.setText(_translate("MainWindow", "Graph"))
        self.btn_naive_bayes.setText(_translate("MainWindow", "Naive Bayes"))
        self.btn_svm.setText(_translate("MainWindow", "SVM"))
        self.btn_logistic_regression.setText(_translate("MainWindow", "L Regression"))
        self.btn_mlp.setText(_translate("MainWindow", "MLP"))
        self.label_2.setText(_translate("MainWindow", "Classification of Urdu Web Queries"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabTrain), _translate("MainWindow", "Train"))
        self.line_edit_file_path_2.setPlaceholderText(_translate("MainWindow", "Enter your Search Query..."))
        self.btn_search_query.setText(_translate("MainWindow", "SEARCH"))
        self.label_4.setText(_translate("MainWindow", "Query Result:"))
        self.label_query_result.setText(_translate("MainWindow", "No Query Entered"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabTest), _translate("MainWindow", "Test"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

sys.exit(app.exec_())

我必须单击按钮才能编写函数,我尝试了许多解决方案,但找不到匹配项。请帮助我是否必须制作一个单独的函数文件,然后导入gui.py文件?

0 个答案:

没有答案