Creating animation by drawing concentric circles & rectangles on Tkinter Canvas using random colour
Getting random colours
We will generate hex code by using RGB values. These RGB values will be taken from random number generator over the range from 0 to 255.
color_c='#%02x%02x%02x' % (randrange(256), randrange(256), randrange(256))
Using timer
We will call the same function ( recursive calling ) by using after() timer.
if (x1<(width/2)):
c1.after(500,my_draw)
else:
return
Restart button
We placed one Restart button to initialize the values of the x1,y1,x2,y2 coordinates.
def restart():
global x1,y1,x2,y2
x1,y1,x2,y2=5,5,width-5,height-5
my_draw()
b1=tk.Button(my_w,text='Restart',command=lambda:restart())
b1.grid(row=2,column=0)
Full code is here
import tkinter as tk
from random import randrange
my_w = tk.Tk()
width,height=410,410 # set the variables
d=str(width)+"x"+str(height+40)
my_w.geometry(d)
c1 = tk.Canvas(my_w, width=width-10, height=height-10)
c1.grid(row=1,column=0,padx=5,pady=5)
x1,y1,x2,y2=5,5,width-5,height-5
gap=25
def my_draw():
global x1,y1,x2,y2
color_c='#%02x%02x%02x' % (randrange(256), randrange(256), randrange(256))
c1.create_oval(x1, y1, x2,y2,fill=color_c)
#c1.create_rectangle(x1, y1, x2,y2,fill=color_c)
x1,y1,x2,y2=x1+gap,y1+gap,x2-gap,y2-gap
print(x1)
if (x1<(width/2)):
c1.after(500,my_draw)
else:
return
my_draw()
def restart():
global x1,y1,x2,y2
x1,y1,x2,y2=5,5,width-5,height-5
my_draw()
b1=tk.Button(my_w,text='Restart',command=lambda:restart())
b1.grid(row=2,column=0)
my_w.mainloop()
« Tkinter Canvas
Moving element in Canvas» Moving widgets or Images on Canvas by using move() →
Sin & cos curves in Canvas»
← Subscribe to our YouTube Channel here