Python time.sleep 第 2 行发生在第 1 行之前

Python time.sleep on line 2 happens before line 1

self.output.clear()
self.output.append("Text that you should see for 2 sec")
time.sleep(2)
self.output.clear()

您应该在 window 中看到文字 2 秒,然后它应该清除并继续,但睡眠发生在文字显示之前,它继续清晰,根本不显示文字。任何人都知道谁来解决这个问题?

当您睡觉时,您可能会阻止渲染发生。尝试设置文本,然后使用定时器回调来清除它。

self.myTimer = QTimer()

self.myTimer.singleShot(2000, lambda: self.output.clear())