如何使代码等待到按钮被按下的PyQt

问题描述 投票:0回答:2

所以这是我有什么,但基本上服务于同一目的的短版:

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

我怎么会去让代码等待到输入再次按下按钮,然后使其再次运行buttonOk?

python pyqt
2个回答
0
投票

添加一个布尔值,看是否已经被按下了。事情是这样的:

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

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_())
© www.soinside.com 2019 - 2024. All rights reserved.