-1

I am relatively new to Python and PyQt5, so sorry if this question is quite simple. I want to create a critical message stating that a particular QlineEdit is empty and so the program does not run. I was able to bind the message to the respective input, however, when I click the cancel button or the 'X' the message keeps popping. I would like it to close after either is clicked, what am I doing wrong? PS: I am using 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')          
Luca
  • 1
  • 3
  • What exactly do you mean by "I was able to bind the message to the respective input"? How is `startTest` being called? – ekhumoro Aug 07 '17 at 16:58
  • 1
    You might want to re-think using all those message boxes, because it will result in a *really horrible* user experience. A much better way to do this is to initially disable the button that does the calculation, and only enable it once all the necessary fields have a valid value. It would also be nicer for the user to enter the numerical values via spin-boxes (which would in turn avoid having to do all those ugly string conversions). – ekhumoro Aug 07 '17 at 17:03
  • ekhumoro StartTest is being called as a button is pressed, and "Yes" I wrote some lines to block the button if all the inputs are not input and it works. However, if the user erases one of the inputs the button is not again disabled, that was the reason for me to create the QMessageBoxes. The code I wrote for it is as follows:` self.start_btn.setEnabled(False) def enablebtn(self): if len(self.Radius_in.text()) == 0: pass else: self.start_btn.setEnabled(True)` – Luca Aug 08 '17 at 08:34
  • I solved the problem, it was a detail that I was missing. Thanks for the attention. – Luca Aug 08 '17 at 09:25

1 Answers1

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)
Luca
  • 1
  • 3