在PyQt中单击确定后,使用QMessageBox并返回主窗体的最佳方式是什么?

问题描述:

我正在使用QMessageBox来告诉用户在提交触发运行的主窗体之前他们输入的字段是不正确还是缺失。目前当QMessageBox弹出时,主窗口消失(我认为它会保留在它后面但是模态),当你点击OK时,整个应用程序关闭。我看了一些例子,但是我不知道我做错了什么。有人可以帮忙吗?在PyQt中单击确定后,使用QMessageBox并返回主窗体的最佳方式是什么?

这里的这段代码:

def isComplete(self): 
    complete = True 

    # check field 
    variable = self.dlg.ui.txtField.text() 

    if variable: 
    # got a non-empty string 
    else: 
    complete = False 
    msgBox = QtGui.QMessageBox() 
    msgBox.setText("Please fill in all required fields") 
    msgBox.exec_() 

    return complete 


def run(self): 
    # show dialog 
    self.dlg.show() 

    # run the dialog event loop 
    result = self.dlg.exec_() 

    # check necessary fields 
    complete = self.isComplete() 

    # see if OK was pressed and fields are complete 
    if (result and complete): 
    self.runCalcs() 
+1

这不是一个完全可运行的例子。我甚至无法确定msgBox是否是主要的孩子。 – user1767754

在简单的情况下,你可以使用静态方法informationquestionwarning和QMessageBox提示的critical。如果指定 ARG是这将是模态:

def isComplete(self): 
    complete = True 

    # check field 
    variable = self.dlg.ui.txtField.text() 

    if variable: 
    # got a non-empty string 
    else: 
    complete = False 
    QtGui.QMessageBox.warning(self, "Warning", "Please fill in all required fields") 

    return complete 


def run(self): 
    # show dialog 
    self.dlg.show() 

    # run the dialog event loop 
    result = self.dlg.exec_() 

    # check necessary fields 
    complete = self.isComplete() 

    # see if OK was pressed and fields are complete 
    if (result and complete): 
    self.runCalcs()