Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Timer without blocking the window in Tkinter

For a project I need to do, I code a Bomberman. I'm working with the software Pyzo, in Python language, and with Tkinter. The problem I have is that I need to a timer for the bomb, for example, I put a bomb and it exploded 3 seconds after. But I have tested with many different things like .after; time module (time.sleep), a loop. The consequence is always the same, the windows freezes and I can't move anymore, but when the loop is finished, the screen is refreshed and players are at new positions.

How can I do a proper timer to permit my bombs to explode after 3 seconds ? Thank you!

like image 277
Sanguis Avatar asked Feb 22 '26 05:02

Sanguis


1 Answers

You can use

widget.after(milliseconds, function, *arguments)

to let the function(*arguments) be called after milliseconds. If the function takes no arguments use widget.after(milliseconds, function). One argument widget.after(milliseconds, function, arg1), ....

widget can be Tk(), Canvas(), Frame(), Label(), ... object.

If you are interested in loops: tkinter loop and serial write

like image 120
User Avatar answered Feb 24 '26 02:02

User