AttributeError:“ Ui_MainWindow”对象没有属性“ btn_click”

时间:2018-09-26 17:14:49

标签: python pyqt attributeerror

尝试调用按钮单击功能时,我一直收到此错误。我尝试了有关属性错误的其他答案,但无法弄清楚我做错了什么。我试图在qlineedit中抓取文本并将其存储在变量training_epochs中,然后调用函数Train。提前致谢。 我的代码如下:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(573, 483)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.Epochs = QtWidgets.QLineEdit(self.centralwidget)
        self.Epochs.setGeometry(QtCore.QRect(120, 120, 113, 20))
        self.Epochs.setObjectName("Epochs")

        def btn_click(self):
            training_epochs=self.Epochs.text()
            Train(training_epochs)
            return

        self.TrainNeuralNetwork = QtWidgets.QPushButton(self.centralwidget)
        self.TrainNeuralNetwork.setGeometry(QtCore.QRect(270, 90, 141, 23))
        self.TrainNeuralNetwork.setObjectName("TrainNeuralNetwork")
        self.TrainNeuralNetwork.clicked.connect(self.btn_click) #error occurs here

        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(240, 240, 256, 192))
        self.graphicsView.setObjectName("graphicsView")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 573, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_2.setText(_translate("MainWindow", "Epochs"))
        self.TrainNeuralNetwork.setText(_translate("MainWindow", "Train Neural Network"))


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_())

0 个答案:

没有答案