【发布时间】: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_())
【问题讨论】: