【问题标题】:QMessageBox does not closeQMessageBox 没有关闭
【发布时间】:2017-08-07 09:06:36
【问题描述】:

我对 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')          

【问题讨论】:

  • “我能够将消息绑定到相应的输入”到底是什么意思? startTest 是如何被调用的?
  • 您可能需要重新考虑使用所有这些消息框,因为这会导致非常糟糕的用户体验。一个更好的方法是首先禁用进行计算的按钮,并且只有在所有必要的字段都具有有效值时才启用它。用户通过旋转框输入数值也会更好(这反过来又可以避免进行所有那些难看的字符串转换)。
  • ekhumoro StartTest 在按下按钮时被调用,并且“是”我写了一些行来阻止按钮,如果所有输入都没有输入并且它可以工作。但是,如果用户删除其中一个输入,则该按钮不会再次被禁用,这就是我创建 QMessageBoxes 的原因。我为它写的代码如下:` self.start_btn.setEnabled(False) def enablebtn(self): if len(self.Radius_in.text()) == 0: pass else: self.start_btn.setEnabled(True )`
  • 我解决了这个问题,这是我遗漏的一个细节。感谢您的关注。

标签: python pyqt pyqt5 qmessagebox


【解决方案1】:
    # 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 2011-01-15
    • 1970-01-01
    • 2015-01-11
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多