2017-08-07 124 views
-1

我對Python和PyQt5比較陌生,所以很抱歉如果這個問題很簡單。我想創建一個關鍵的消息,說明一個特定的QlineEdit是空的,所以程序不運行。我能夠將消息綁定到相應的輸入,但是,當我單擊取消按鈕或「X」時,消息不斷彈出。我想在點擊後關閉它,我做錯了什麼? PS:我正在使用PyQt5。QMessageBox不關閉

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')   
+0

究竟你「我能夠將消息綁定到相應的輸入」是什麼意思?如何調用startTest? – ekhumoro

+1

您可能想要重新考慮使用所有這些消息框,因爲這會導致非常糟糕的用戶體驗。一個更好的方法是最初禁用執行計算的按鈕,並且只有在所有必需字段都有有效值時才啓用它。用戶通過旋轉框輸入數值也會更好(這樣可以避免必須進行所有那些難看的字符串轉換)。 – ekhumoro

+0

ekhumoro StartTest被稱爲一個按鈕被按下,「是」我寫了一些行來阻止按鈕,如果所有的輸入都沒有輸入,它的工作原理。但是,如果用戶刪除其中一個輸入,則該按鈕不會再次被禁用,這就是我創建QMessageBox的原因。我爲它寫的代碼如下:'self.start_btn.setEnabled(False) def enablebtn(self): if len(self.Radius_in.text())== 0: pass else: self。 start_btn.setEnabled(True)' – Luca

回答

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)