[Tutor] Tkinter mainloop()

Francesco Loffredo ilcomputertrasparente at gmail.com
Mon Jul 5 21:00:05 CEST 2010

Hello all, this is the first time I ask for advice but I've been lurking 
for some month and i'm sure I'll find more than I need.
I'm learning Python and Tkinter, and I chose an old board game as a 
practice field. I used a Canvas and many Polygons, one for each 
hexagonal space of the board, and I bound a mouse click to a PlacePiece 
function that handles the move. I managed to make it work for two human 
players, but when it came to let the computer play, I found a problem.
I made a slightly different function, called Auto_PlacePiece, that 
doesn't get the target hex from an Event, but from arguments given, and 
I had to figure when this function has to be called. I thought an 
obvious place is at the end of the move, whether it's a manual or auto 
one. Just after having switched from a player to the other, if the new 
current player is "COMPUTER" then an automatic move is triggered.
So, where's the problem? The problem is that the Canvas is refreshed, 
and the previous move becomes visible to the players, *only after the 
completion of the automatic move!* This is, I think, a consequence of 
the binding and a feature of Tk's mainloop(): the loop waits for the 
bound routine to end before redrawing the graphics, and unfortunately I 
call the automatic move inside (albeit at the very end) of the previous 
move. And now for the (long awaited) question:
How can I ask a Canvas to redraw itself at my command? And if i can't, 
when should I call the auto move?

More information about the Tutor mailing list