【问题标题】:How to get result from corfirmation dialog如何从确认对话框中获取结果
【发布时间】:2017-12-20 11:11:31
【问题描述】:

我对弹出窗口的结果有疑问。下面我展示了我的部分代码来理解这个问题。

这是一种用户在 GUI 中做出选择的弹出窗口。在此之后,它应该会显示一个窗口,其中会出现“你确定吗?”的问题,以及“是”和“否”两个按钮。

问题是,当我测试下面的代码时(msg.show() 之前和之后),我设置的值与False 相同。

为什么不这样工作:

  • 函数前 -> False
  • 显示我的窗口并等待点击按钮
  • 如果我点击“是”按钮,则给True,否则给False

我该如何正确处理这个问题?还有其他方法吗?

from PyQt4 import QtCore, QtGui
from Message import Ui_Message
import sys

class MessageBox(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent=None)
        self.msg = Ui_Message()
        self.msg.setupUi(self)
        self.confirmed=False
        self.declined=False

        QtCore.QObject.connect(self.msg.NoButton, QtCore.SIGNAL(("clicked()")), self.Declined)
        QtCore.QObject.connect(self.msg.YesButton, QtCore.SIGNAL(("clicked()")), self.Confirmed)

    def Confirmed(self):
        self.confirmed = True
        MessageBox.close(self)
        return True

    def Declined(self):
        self.declined = True
        MessageBox.close(self)



if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    msg = MessageBox()
    print('Befor show window',msg.confirmed)
    msg.show()
    print('After show window', msg.confirmed)
    sys.exit(app.exec_())

【问题讨论】:

    标签: python dialog pyqt pyqt4


    【解决方案1】:

    您的示例不起作用,因为您正在打印“显示窗口之后”之前窗口已关闭。阻塞的是 exec() 方法,而不是 show() 方法,因此您的示例需要这样编写:

    app = QtGui.QApplication(sys.argv)
    msg = MessageBox()
    print('Before show window', msg.confirmed)
    msg.show()
    app.exec_() # this blocks, waiting for close
    print('After show window', msg.confirmed)
    sys.exit()
    

    然而,一个更现实的例子展示了如何使用对话框来确认一个动作是这样的:

    import sys
    from PyQt4 import QtCore, QtGui
    
    class MessageBox(QtGui.QDialog):
        def __init__(self, parent=None):
            super(MessageBox, self).__init__(parent)
            self.yesButton = QtGui.QPushButton('Yes')
            self.noButton = QtGui.QPushButton('No')
            layout = QtGui.QGridLayout(self)
            layout.addWidget(QtGui.QLabel('Are you sure?'), 0, 0)
            layout.addWidget(self.yesButton, 1, 0)
            layout.addWidget(self.noButton, 1, 1)
            self.yesButton.clicked.connect(self.accept)
            self.noButton.clicked.connect(self.reject)
    
    class Window(QtGui.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.button = QtGui.QPushButton('Do Something')
            self.button.clicked.connect(self.handleButton)
            layout = QtGui.QVBoxLayout(self)
            layout.addWidget(self.button)
    
        def handleButton(self):
            if self.confirmSomething():
                print('Yes')
            else:
                print('No')
    
        def confirmSomething(self):
            msg = MessageBox(self)
            result = msg.exec_() == QtGui.QDialog.Accepted
            msg.deleteLater()
            return result
    
    if __name__ == "__main__":
    
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        app.exec_()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2010-10-27
      相关资源
      最近更新 更多