【问题标题】:Sleep is not working on pyqt4睡眠在pyqt4上不起作用
【发布时间】:2013-05-23 22:52:24
【问题描述】:

我遇到了这个问题。我正在尝试在 pyqt4 上的 lineEdit 对象上设置文本,然后等待几秒钟并更改同一 lineEdit 的文本。为此,我正在使用 python Time 模块上给出的time.sleep() 函数。但我的问题是,不是设置文本,然后等待并最终重写 lineEdit 上的文本,它只是等待它应该休眠的时间,只显示最终文本。我的代码如下:

from PyQt4 import QtGui
from gui import *

class Ventana(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.button.clicked.connect(self.testSleep)

    def testSleep(self):
        import time   
        self.lineEdit.setText('Start')
        time.sleep(2)
        self.lineEdit.setText('Stop')        

    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_())

【问题讨论】:

    标签: python qt python-3.x pyqt sleep


    【解决方案1】:

    您不能在此处使用time.sleep,因为这会冻结 GUI 线程,因此在此期间 GUI 将完全冻结。

    您可能应该使用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_())
    

    【讨论】:

      【解决方案2】:

      另外,看看 QThread sleep() 函数,它使当前线程进入睡眠状态并允许其他线程运行。 https://doc.qt.io/qt-5/qthread.html#sleep

      【讨论】:

        【解决方案3】:

        你不能在这里使用 time.sleep 因为那会冻结 GUI 线程,所以 GUI 将在这段时间内完全冻结。你可以使用 QtTest 模块而不是 time.sleep()。

        from PyQt4 import QtTest
        
        QtTest.QTest.qWait(msecs)
        

        所以你的代码应该是这样的:

        from PyQt4 import QtGui,QtTest
        from gui import *
        
        class Ventana(QtGui.QMainWindow, Ui_MainWindow):
            def __init__(self, parent=None):
                QtGui.QWidget.__init__(self, parent)
                self.setupUi(self)
                self.button.clicked.connect(self.testSleep)
        
            def testSleep(self):
                import time   
                self.lineEdit.setText('Start')
                QtTest.QTest.qWait(2000)
                self.lineEdit.setText('Stop')        
        
            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_())
        

        【讨论】:

        • @eyllanesc:请告诉我这段代码是否有问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多