所以这是我所拥有的简短版本,但基本上用于同一目的:
import sys
from PyQt4 import QtGui, QtCore
def __init__(self):
super(PyQt, self).__init()
self.gridLayout = QtGui.QGridLayout()
self.setGeometry(200,200,200,200)
self.Enter = QtGui.QPushButton("Enter")
self.gridLayout.addWidget(self.Enter,1,0,0,0)
self.Enter.clicked.connect(self.buttonOK)
self.lineEdit = QtGui.QLineEdit()
self.gridLayout.addWidget(self.lineEdit,0,0,0,0)
def buttonOK(self):
# if statement checking if lineEdit.is_int() == False
question = QtGui.QMessageBox.question(self, "The value that you entered is not a whole number. Please enter again", QtGui.QMessageBox.OK | QtGui.QMessageBox.Ignore)
if question == QtGui.QMessageBox.OK
# Make the code wait for "Enter" button is pressed again
我如何让代码等到再次按下Enter按钮,然后让它再次运行buttonOk?
答案 0 :(得分:0)
添加一个bool以查看它是否已被按下。像这样:
def __init__(self):
# ...
self.pushed = False
def buttonOK(self):
# if statement checking if lineEdit.is_int() == False
question = QtGui.QMessageBox.question(self, "The value that you entered is not a whole number. Please enter again", QtGui.QMessageBox.OK | QtGui.QMessageBox.Ignore)
if question == QtGui.QMessageBox.OK
if self.pushed = True:
# Second time it was pushed
else:
self.pushed = True
答案 1 :(得分:0)
我建议使用QDialog而不是QMessageBox。试试这个:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class MyAlert(QtGui.QDialog):
'''
This alert window used to display messages
and wait for user action to continue with the rest of the code
'''
text = ''
# type = 'warning' # used for image
def __init__(self, text, parent=None, *args, **kwargs):
super(MyAlert, self).__init__(parent)
self.text = text
# self.text = kwargs.pop('text', '')
self.setupUi()
def setupUi(self):
self.setObjectName(_fromUtf8("Dialog"))
self.resize(264, 130)
self.gridLayout = QtGui.QGridLayout(self)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_2 = QtGui.QLabel(self)
self.label_2.setMaximumSize(QtCore.QSize(60, 16777215))
self.label_2.setText(_fromUtf8(""))
self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../Images/Icons/warning.png")))
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout_2.addWidget(self.label_2)
self.label_3 = QtGui.QLabel(self)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout_2.addWidget(self.label_3)
self.gridLayout.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
self.retranslateUi()
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), self.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), self.reject)
QtCore.QMetaObject.connectSlotsByName(self)
self.alert()
def retranslateUi(self):
self.setWindowTitle(_translate("Dialog", "Alert", None))
self.label_3.setText(_translate("Dialog", self.text, None))
def alert(self):
self.show()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = MyAlert("test")
if(ui.exec_() == QtGui.QDialog.Accepted):
print 'ok'
else:
print 'NOT ok'
sys.exit(app.exec_())