ARTICLE AD BOX
In PyQt, you should not use time.sleep() in code that interacts with the GUI or event loop. Even if you put it in a thread, this pattern is fragile and unnecessary for animations.
and Qt already provides non-blocking timers that do exactly what you want. You can use QTimer.singleShot() to wait without blocking the event loop or freezing the UI.
from PyQt5.QtCore import QTimer def step_1(): print("Animation finished") # Wait 3 seconds, then continue QTimer.singleShot(3000, step_1)This:
does not block the GUI lets other code continue running and is fully PyQt-safeand the Problems with time.sleep() is that:
it blocks execution Threads + GUI = race conditions No integration with Qt’s event loop and animations may stutter or freezeSo, in a nutshell, should avoid time.sleep() and instead use QTimer which is non-blocking and safe
4,5667 gold badges20 silver badges31 bronze badges
In PyQt, you should not use time.sleep() for waiting, especially when animations or the GUI are involved. Even when used in a separate thread, it can cause freezes, stuttering animations, and race conditions.
Qt already provides a non-blocking solution that works with the event loop: QTimer.
from PyQt5.QtCore import QTimer def animation_finished(): print("Animation finished") # Wait 3 seconds without blocking the GUI QTimer.singleShot(3000, animation_finished)This approach:
• does not block the GUI
• allows other code to keep running
• is safe for animations
• is fully integrated with Qt’s event loop
Conclusion: Avoid time.sleep() in PyQt applications. Use QTimer for delays and animation timing.
New contributor
Akash Ghosh is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Explore related questions
See similar questions with these tags.
