【问题标题】:How to create custom PyQt accept/reject dialogue?如何创建自定义 PyQt 接受/拒绝对话?
【发布时间】:2015-08-07 12:16:33
【问题描述】:

我正在努力弄清楚如何创建自定义接受/拒绝对话。我有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'

【问题讨论】:

    标签: python dialog pyqt


    【解决方案1】:

    使用 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 函数并通过例如调用它一个按钮。

    【讨论】:

      【解决方案2】:

      感谢@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_())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-09
        • 2010-10-21
        • 2019-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多