D
D
Dmitry012016-01-18 02:04:57
Python
Dmitry01, 2016-01-18 02:04:57

The question is, why does the python in the While loop not draw objects?

task: to implement the animation of moving the circle.

from tkinter import *
import time 

root = Tk()

canvas = Canvas(root,width=1000,height=500)
x=0
y=0
canvas.create_oval(x,y,x+10,y+10)
start_time = time.time()
sec = 10
while (time.time()-start_time) <= sec:
  canvas.delete('all')
  x=20*(time.time()-start_time)
  y=30*(time.time()-start_time)
  canvas.create_oval(x,y,x+10,y+10)

canvas.pack()
root.mainloop()

but when I run the script nothing happens for 10 seconds, and then the result appears in the form of a window and a circle at the end point x=200,y=300 but there was no animation itself, what could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2016-01-18
@AMar4enko

Because the processing of all events in a window application, including window redrawing, occurs in the event loop. You "stop" it with your cycle.

K
Konstantin Tropin, 2017-12-15
@KonTropin

Try adding a canvas update canvas.update()
from tkinter import *
import time
root = Tk()
canvas = Canvas(root,width=1000,height=500)
x=0
y=0
canvas.create_oval(x,y,x+ 10,y+10)
canvas.pack()
start_time = time.time()
sec = 10
while (time.time()-start_time) <= sec:
canvas.delete('all')
x=20*(time. time()-start_time)
y=30*(time.time()-start_time)
canvas.create_oval(x,y,x+10,y+10)
canvas.update()
root.mainloop()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question