在 PyQt 中单击“确定”后,使用 QMessageBox 和 return 返回主窗体的最佳方法是什么?

What's the best way to use a QMessageBox and return back to main form after clicking OK in PyQt?

在提交触发 运行 的主表单之前,我使用 QMessageBox 告诉用户他们输入的字段是否不正确或缺失。当前,当 QMessageBox 弹出时,主 window 消失(我认为它会留在它后面但模态),当您单击“确定”时,整个应用程序关闭。我看过例子,但我不知道我做错了什么。有人可以帮忙吗?

这是这段代码:

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()

在简单的情况下,您可以使用 QMessageBox 的静态方法 information, question, warning and critical。如果指定 parent 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()