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

时间:2017-08-29 16:18:36

标签: python pyqt pyqt4 qgis

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

这是代码的一部分:

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 个答案:

答案 0 :(得分:1)

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