0
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?

Marcus
  • 13
  • 4
  • 2
    normally you cannot use `sleep` together with any GUI. What framework do you use, how is the text displayed? – Daniel Jun 25 '15 at 15:44
  • What is `self.output`? I've never seen that way of doing output before. That probably has something to do with it. – Linuxios Jun 25 '15 at 15:44
  • I use QtGui, the self.output is the window where the text shows. – Marcus Jun 25 '15 at 15:46
  • I agree with Daniel. Most GUI frameworks only update the contents of the window when the function you're running ends and control returns to the main loop. So `sleep` just makes the window hang, with none of your changes being visible until it's done. – Kevin Jun 25 '15 at 15:48
  • Quick search and I found a [possible solution](http://stackoverflow.com/questions/16801007/sleep-is-not-working-on-pyqt4) with this GUI framework. – Brobin Jun 25 '15 at 15:48

2 Answers2

1
self.myTimer = QTimer()

self.myTimer.singleShot(2000, lambda: self.output.clear())
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Marcus
  • 13
  • 4
  • Don't put "Solved" into your answer, mark it as accepted instead. See [Accepting Answers: How does it work?](http://meta.stackexchange.com/a/5235/137096). – jfs Jun 26 '15 at 15:26
0

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