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

时间:2015-08-07 12:16:34

标签: python dialog pyqt

我正在努力弄清楚如何创建自定义接受/拒绝对话。我有WarningMessage()课,我希望能够以两种模式工作:

  • 只需告知用户出现问题(只有确定按钮才能关闭对话框。)
  • 从用户那里得到决定:要么继续要么取消(两个按钮)。该决定应该是班级的输出: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'

2 个答案:

答案 0 :(得分:2)

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

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()


    def initUI(self):               

        self.setGeometry(300, 300, 250, 150)        
        self.setWindowTitle('Message box')    
        self.show()


    def closeEvent(self, event):

        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()        


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

您可以轻松地更改此示例中的closeEvent函数,并通过以下方式调用它:一个按钮。

答案 1 :(得分:0)

感谢@ a.smiet,我能够按照我需要的方式修改课程,现在我可以通过这种方式让用户选择:choice = WarningMessage(u'Continue?', choice = True).user_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_())