Skip to content Skip to sidebar Skip to footer

Sleep In Tkinter (python2)

I search to make a sleep in a while loop, in an tkinter's canvas. In Python2 The aim is to have a randomly moving point, refreshed every X seconds (then .I'll be able a bigger scri

Solution 1:

sleep does not mix well with Tkinter because it makes the event loop halt, which in turn makes the window lock up and become unresponsive to user input. The usual way to make something happen every X seconds is to put the after call inside the very function you're passing to after. Try:

import Tkinter, time

x1, y1, x2, y2 = 10, 10, 10, 10
def affichage():
    global x1, y1, x2, y2
    can1.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
def affichage2():
    global x1, y1, x2, y2
    can1.delete("all")
    can1.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
    x1 += 10
    y1 += 10
    x2 += 10
    y2 += 10
    can1.after(1000, affichage2)

fen1 = Tkinter.Tk()
can1 = Tkinter.Canvas(fen1, height=200, width=200)
affichage()
can1.pack()
temps = 3000

can1.after(1000, affichage2)
fen1.mainloop()

fen1.destroy()

Post a Comment for "Sleep In Tkinter (python2)"