[英]Python, PySide sleep without stalling the whole program?
I have this piece of code: 我有这段代码:
self.connect(self.Yes_button,SIGNAL('clicked()'),self.Yes_pressed)
self.connect(self.No_button,SIGNAL('clicked()'),self.No_pressed)
def Yes_pressed(self):
self.Box.append("Hello")
time.sleep(2)
self.Box.append("Text2")
what it does is when the Yes button is pressed it waits 2 seconds first then appends both Hello and Text2(Box is a QTextBrowser() object)How could I make so it would appending one, wait 2 seconds and append the other one instead? 它的作用是当按下Yes按钮时它先等待2秒然后追加Hello和Text2(Box是一个QTextBrowser()对象)我怎么能这样做它会附加一个,等待2秒而是追加另一个呢? Is there a simple solution to this? 有一个简单的解决方案吗?
You can do something like this, using pyqt's
Qtimer
. 你可以使用pyqt's
Qtimer
做这样的事情。 More specifically the singleShot
更具体地说是singleShot
Setup : QTimer.singleShot (int msec, QObject receiver, SLOT()SLOT() member)
设置: QTimer.singleShot (int msec, QObject receiver, SLOT()SLOT() member)
QtCore.QTimer.singleShot(1000, lambda: self.Box.append("Text2")) #1000 milliseconds = 1 second
Or you can event try this implementation: 或者你可以尝试这个实现的事件:
self.timerScreen = QTimer()
self.timerScreen.setInterval(1000) #1000 milliseconds = 1 second
self.timerScreen.setSingleShot(True)
self.timerScreen.timeout.connect(self.Box.append("Text2"))
Part 2 第2部分
You can probably do something like this, but I do not recommend it: 你可能会做这样的事情,但我不推荐它:
def testSleep(self):
self.lineEdit.setText('Start')
QtCore.QTimer.singleShot(10000, lambda: self.Box.append("Text2"))
QtCore.QTimer.singleShot(10000,lambda: self.timerEvent)
def timerEvent(self):
QtCore.QTimer.singleShot(10000, lambda: self.Box.append("Text3"))
You are better off just doing something like this: 你最好做这样的事情:
x = 10000 #or whatever number
QtCore.QTimer.singleShot(10000 + x, lambda: self.Box.append("Text3"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.