Python time.sleep on line 2 happens before line 1

103 Views Asked by At
self.output.clear()
self.output.append("Text that you should see for 2 sec")
time.sleep(2)
self.output.clear()

You are supposed to see the text in a window for 2 seconds then it should clear and continue but the sleep happens before the text shows and it continues to the clear, not showing the text at all. Anyone know who to fix this?

2

There are 2 best solutions below

0
Marcus On BEST ANSWER
self.myTimer = QTimer()

self.myTimer.singleShot(2000, lambda: self.output.clear())
0
George McCollister On

When you sleep you're probably preventing the rendering from taking place. Try setting the text, then using a timer callback to clear it.