简体   繁体   English

Python,PySide睡眠没有拖延整个程序?

[英]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.

相关问题 提升python睡眠包装器使整个python程序进入睡眠状态 - boost python sleep wrapper causes whole python program to sleep 如何在一个 function 中睡觉而不睡觉整个程序 - how to sleep in one function without sleeping the whole program PySide,睡几次而不冻结窗户 - PySide, sleep several times without freeze the window 使用PySide将python程序制作为可执行文件 - Make a python program with PySide an executable 如何制作一个每 24 小时执行一次的函数而不用 time.sleep() 停止整个程序? - How to make a function that is done every 24 hours without stopping the whole program with time.sleep() for that time? vtkRenderWindowInteractor停止程序 - vtkRenderWindowInteractor stalling program python 多处理在不终止整个程序的情况下第二次运行速度较慢 - python multiprocessing runs slower at second time without terminating whole program 如何在不停止整个程序的情况下延迟python - How Do you make a delay in python without stoping the whole program python如何循环一个函数等待而不暂停整个程序 - python how to loop a function with wait without pausing the whole program 如何在不结束整个程序的情况下退出 function? (Python) - How can I exit a function without ending the whole program? (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM