Moving a window on the screen
Peter Otten
__peter__ at web.de
Thu Nov 6 04:15:20 EST 2014
ast wrote:
> Why the following program doesn't work ?
>
> for x in range(0, 100, 10):
> fen.geometry("200x200+%d+10" % x)
> time.sleep(0.5)
>
>
> where fen is a window (fen = Tk())
>
> The fen window goes from it's initial location to the last one
> but we dont see all the intermediate steps
As Chris says, using sleep() with a gui program usually doesn't work. While
the script is sleeping the event loop doesn't run and consequently the user
will not see any updates of the program's state.
Instead of sleeping you have to schedule events that trigger a callback and
then go back to the event loop. Such a callback may schedule another
invocation of itself, thus giving you the desired effect:
import tkinter as tk
root = tk.Tk()
xpositions = iter(range(0, 100, 10))
def move_next():
try:
x = next(xpositions)
except StopIteration:
pass
else:
root.geometry("200x200+%d+10" % x)
root.after(500, move_next) # have tkinter call move_next again
# in about 500 milliseconds
root.after_idle(move_next)
root.mainloop()
More information about the Python-list
mailing list