QMessageBox没有关闭

时间:2017-08-07 09:06:36

标签: python pyqt pyqt5 qmessagebox

我对Python和PyQt5比较陌生,如果这个问题很简单,那就很抱歉。我想创建一条关键消息,指出特定的QlineEdit为空,因此程序不会运行。我能够将消息绑定到相应的输入,但是,当我单击取消按钮或“' X'消息不断弹出。点击之后我想关闭它,我做错了什么? PS:我正在使用PyQt5。

def startTest(self):
    if len(self.Radius_in.text()) == 0:
        QMessageBox.critical(self, "Error", "Please enter the radius", QMessageBox.Cancel)
        pass
    elif len(self.Distance_in.text()) == 0:
        QMessageBox.critical(self, "Error", "Please enter the distance",QMessageBox.Cancel)
        pass
    elif len(self.Speed_in.text()) == 0:
        QMessageBox.critical(self, "Error", "Please enter the linear speed",QMessageBox.Cancel)
        pass
    elif len(self.Nload_in.text()) == 0:
        QMessageBox.critical(self, "Error", "Please enter the normal load",QMessageBox.Cancel)
        pass
    elif len(self.Acq_int_in.text()) == 0:
        QMessageBox.critical(self, "Error", "Please enter the acquisition rate",QMessageBox.Cancel)
        pass
    elif len(self.file_name.text()) == 0:
        QMessageBox.critical(self, "Error", "Please enter the name of the file",QMessageBox.Cancel)
        pass     
    else:
        #open and save a file
        fileName = self.file_name.text()
        savedFile = open(fileName + ".txt","w")

        runTime = round(float(str(self.Distance_in.text()))/(60.*float(str(self.Speed_in.text()))),2)
        NoLaps = round(float(str(self.Distance_in.text()))/(pi*2.*float(str(self.Radius_in.text()))),2)
        discRot = round(float(str(self.Speed_in.text()))*60./(2.*pi*float(str(self.Radius_in.text()))),2)
        discFreq = round(float(str(self.Speed_in.text()))/(2.*pi*float(str(self.Radius_in.text()))),2)                   
        try:
            self.duration_out.setText(str(runTime))
            self.laps_out.setText(str(NoLaps))
            self.rotation_out.setText(str(discRot))
            self.Freq_out.setText(str(discFreq))
        except:
            self.duration_out.setText('error')
            self.laps_out.setText('error')
            self.rotation_out.setText('error')
            self.Freq_out.setText('error')          

1 个答案:

答案 0 :(得分:0)

    # Enable and disable the start button    
    self.start_btn.setEnabled(False)
def enablebtn(self):
    if (len(self.Radius_in.text()) != 0) and (len(self.Distance_in.text()) != 0) and (len(self.Speed_in.text()) != 0)\
        and (len(self.Nload_in.text()) != 0) and (len(self.Acq_int_in.text()) != 0) and (len(self.file_name.text()) != 0): 
            self.start_btn.setEnabled(True)
    else:
        self.start_btn.setEnabled(False)