In: Computer Science
Create a Python program file and . Your
program will draw random circles with filled and non-filled color
(random colors) and rectangles, fill andnon-fill color (also random
colors) on the canvas. At least 10 for each category
NORMAL CODE WHICH CAN BE DONE USING PYTHON
1.For Random rectangles:-
from tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width=400,height=400)
def random_rectangle(width, height):
x1 = random.randrange(width)
y1 = random.randrange(height)
x2 = random.randrange(x1 + random.randrange(width))
y2 = random.randrange(y1 + random.randrange(height))
canvas.create_rectangle(x1, y1, x2, y2)
random_rectangle(400, 400)
for x in range(0, 100):
random_rectangle(400, 400)
2.For Coloured rectangles:-
from tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width=400,height=400)
def random_rectangle(width, height, fill_color):
x1 = random.randrange(width)
y1 = random.randrange(height)
x2 = random.randrange(x1 + random.randrange(width))
y2 = random.randrange(y1 + random.randrange(height))
canvas.create_rectangle(x1, y1, x2, y2, fill=fill_color)
random_rectangle(400, 400, 'green')
random_rectangle(400, 400, 'red')
random_rectangle(400, 400, 'blue')
random_rectangle(400, 400, 'orange')
random_rectangle(400, 400, 'yellow')
random_rectangle(400, 400, 'pink')
random_rectangle(400, 400, 'purple')
random_rectangle(400, 400, 'violet')
random_rectangle(400, 400, 'magenta')
random_rectangle(400, 400, 'cyan')
random_rectangle(400, 400, '#ffd800')
3.For random circles:
from Tkinter import *
import random
import time
def idle5(dummy):
"""freeze the action for 5 seconds"""
root.title("Idle for 5 seconds")
time.sleep(5)
root.title("Happy Circles ...")
# create the window form
root = Tk()
# window title text
root.title("Happy Circles ...")
# set width and height
w = 640
h = 480
# create the canvas for drawing
cv = Canvas(width=w, height=h, bg='black')
cv.pack()
# list of colors to pick from
colorList = ["blue", "red", "green", "white", "yellow", "magenta", "orange"]
# endless loop to draw the random circles
while 1:
# random center (x,y) and radius r
x = random.randint(0, w)
y = random.randint(0, h)
r = random.randint(5, 50)
# pick the color
color = random.choice(colorList)
# now draw the circle
cv.create_oval(x, y, x+r, y+r, fill=color)
# update the window
root.update()
# bind left mouse click, idle for 5 seconds
cv.bind('<Button-1>', idle5)
# start the program's event loop
root.mainloop()