你不能time.sleep
在这里使用因为冻结了GUI线程,所以在这段时间内GUI将被完全冻结.
您应该使用a QTimer
并使用它的timeout
信号来安排延迟交付的信号,或者它的singleShot
方法.
例如(调整代码使其无依赖性运行):
from PyQt4 import QtGui, QtCore class Ventana(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setLayout(QtGui.QVBoxLayout()) self.lineEdit = QtGui.QLineEdit(self) self.button = QtGui.QPushButton('clickme', self) self.layout().addWidget(self.lineEdit) self.layout().addWidget(self.button) self.button.clicked.connect(self.testSleep) def testSleep(self): self.lineEdit.setText('Start') QtCore.QTimer.singleShot(2000, lambda: self.lineEdit.setText('End')) def mainLoop(self, app ): sys.exit( app.exec_()) if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) window = Ventana() window.show() sys.exit(app.exec_())