Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何创建自定义PyQt接受/拒绝对话?_Python_Dialog_Pyqt - Fatal编程技术网

Python 如何创建自定义PyQt接受/拒绝对话?

Python 如何创建自定义PyQt接受/拒绝对话?,python,dialog,pyqt,Python,Dialog,Pyqt,我正在努力找出如何创建自定义的接受/拒绝对话。我有一个WarningMessage()类,我希望能够在两种模式下工作: 只需通知用户有问题(只有Ok按钮关闭对话框) 从用户处获取决定:继续或取消(两个按钮)。此决定应该是类的输出:choise=WarningMessage(u'continue?',choice=True) 第一种模式运行良好,但我无法使第二种模式运行 from PyQt4 import QtGui from PyQt4.QtCore import * class War

我正在努力找出如何创建自定义的接受/拒绝对话。我有一个
WarningMessage()
类,我希望能够在两种模式下工作:

  • 只需通知用户有问题(只有Ok按钮关闭对话框)
  • 从用户处获取决定:继续或取消(两个按钮)。此决定应该是类的输出:
    choise=WarningMessage(u'continue?',choice=True)
第一种模式运行良好,但我无法使第二种模式运行

from PyQt4 import QtGui
from PyQt4.QtCore import *


class WarningMessage(QtGui.QMessageBox):
  '''
  Creates a message for the case when something is wrong
  '''
  def __init__(self, message, choice=False):
    super(WarningMessage, self).__init__()
    self.choice = choice
    if not isinstance(message, basestring):
      message = str(message)
    self.initUI(message)

  def initUI(self, message):
    if not self.choice:
      self.warning(self, u'warning!',
          message, QtGui.QMessageBox.Ok)
      return None
    else:
      self.m_button_box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel, Qt.Horizontal, self)
      QtGui.QVBoxLayout(self).addWidget(self.m_button_box)
      self.m_button_box.accepted.connect(lambda: self.accept(True))
      self.m_button_box.rejected.connect(lambda: self.accept(False))
      self.warning(self, u'Warning!', message, self.m_button_box)

  def accept(self, yes=True):
    if yes:
      return True
    else:
      return False

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    choice = WarningMessage(u'Continue?', choice = True)
    print choice
    sys.exit(app.exec_())
这将抛出错误:

TypeError: arguments did not match any overloaded call:
  QMessageBox.warning(QWidget, QString, QString, QMessageBox.StandardButtons buttons=QMessageBox.Ok, QMessageBox.StandardButton defaultButton=QMessageBox.NoButton): argument 4 has unexpected type 'QDialogButtonBox'
  QMessageBox.warning(QWidget, QString, QString, int, int, int button2=0): argument 4 has unexpected type 'QDialogButtonBox'
  QMessageBox.warning(QWidget, QString, QString, QString, QString button1Text=QString(), QString button2Text=QString(), int defaultButtonNumber=0, int escapeButtonNumber=-1): argument 4 has unexpected type 'QDialogButtonBox'

使用QtGui.QMessageBox.question,如已解释


您可以从本例中轻松更改closeEvent函数,并通过按钮调用它。

多亏@a.smiet,我可以按需要修改我的类,现在我可以让用户选择这种方式:
choice=WarningMessage(u'Continue',choice=True)。user\u choice

from PyQt4 import QtGui
from PyQt4.QtCore import *

class WarningMessage(QtGui.QMessageBox):
  '''
  Creates a message for the case when something is wrong
  '''
  def __init__(self, message, choice=False):
    super(WarningMessage, self).__init__()
    self.choice = choice
    if not isinstance(message, basestring):
      self.message = str(message)
    else:
      self.message = message
    self.user_choice = None
    self.initUI()


  def initUI(self):
    if not self.choice:
      self.warning(self, u'Warning!',
          self.message, QtGui.QMessageBox.Ok)
      return None
    else:
      reply = QtGui.QMessageBox.question(self, u'Warning!',
            self.message, QtGui.QMessageBox.Yes |
            QtGui.QMessageBox.No)

      if reply == QtGui.QMessageBox.Yes:
        self.user_choice = True
      else:
        self.user_choice = False

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    choice = WarningMessage(u'Continue?', choice = True).user_choice
    print choice
    sys.exit(app.exec_())
from PyQt4 import QtGui
from PyQt4.QtCore import *

class WarningMessage(QtGui.QMessageBox):
  '''
  Creates a message for the case when something is wrong
  '''
  def __init__(self, message, choice=False):
    super(WarningMessage, self).__init__()
    self.choice = choice
    if not isinstance(message, basestring):
      self.message = str(message)
    else:
      self.message = message
    self.user_choice = None
    self.initUI()


  def initUI(self):
    if not self.choice:
      self.warning(self, u'Warning!',
          self.message, QtGui.QMessageBox.Ok)
      return None
    else:
      reply = QtGui.QMessageBox.question(self, u'Warning!',
            self.message, QtGui.QMessageBox.Yes |
            QtGui.QMessageBox.No)

      if reply == QtGui.QMessageBox.Yes:
        self.user_choice = True
      else:
        self.user_choice = False

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    choice = WarningMessage(u'Continue?', choice = True).user_choice
    print choice
    sys.exit(app.exec_())