In: Computer Science
In Python, gonna need this ASAP: Complete the draw_pattern(a_canvas, colours_dictionary, pattern_list, size, left, top) function. This function is passed SIX parameters: the Canvas object, a colours dictionary, a list of String elements, and followed by three integer parameters. The function draws a grid of coloured rectangles inside the canvas area. The left-top position of the grid of coloured rectangles is given by the last two parameter values and the size of each rectangle is given by the size parameter. Once you have completed this function you should see rows of coloured rectangles in the canvas area.
def draw_pattern(a_canvas, colours_dictionary, pattern_list,
size, left, top):
down = top
Function:
#Implementation of draw_pattern method
def draw_pattern(a_canvas, colours_dictionary, pattern_list, size,left, top):
#Iterate the loop
for pattern in pattern_list:
#assign pattern[0] to strValue
strValue = pattern[0]
#store the value in requiredcolor
requiredcolor = colours_dictionary[strValue]
#assign left to direction
direction = left
#assign pattern[1] to intValue
intValue = pattern[1]
#Iterate the loop
for each in range(intValue):
#call rectangle method through canavas a_canvas
a_canvas.create_rectangle(direction, top, direction + size, top + size, fill=requiredcolor)
#calculate direction
direction = direction + size
#calcualte top
top = top + size
Code Image:
Sample Output:
Code to Copy:
from tkinter import *
import random
#Implementation of draw_pattern method
def draw_pattern(a_canvas, colours_dictionary, pattern_list, size,left, top):
#Iterate the loop
for pattern in pattern_list:
#assign pattern[0] to strValue
strValue = pattern[0]
#store the value in requiredcolor
requiredcolor = colours_dictionary[strValue]
#assign left to direction
direction = left
#assign pattern[1] to intValue
intValue = pattern[1]
#Iterate the loop
for each in range(intValue):
#call rectangle method through canavas a_canvas
a_canvas.create_rectangle(direction, top, direction + size, top + size, fill=requiredcolor)
#calculate direction
direction = direction + size
#calcualte top
top = top + size
#Implementation of main function
def main():
#Declare size and initlaize with 10
size=10
#assign 2*size to left
left = 2*size
top = 2*size
#Declare pattern_list
pattern_list = [('4', 3), ('1', 10), ('2', 10), ('3', 10), ('4', 3)]
#Declare colours_dictionary as dictionary
colours_dictionary = {"1": "red", "2": "blue", "3": "green", "4": "yellow"}
#calculate length of pattern_list and assign to number_of_rows
number_of_rows = len(pattern_list)
#calculate length of pattern_list[0] and assign to number_of_columns
number_of_columns = len(pattern_list[0])
#calculate canvas_width
canvas_width = size*number_of_columns + size*4
#calcualte canvas_height
canvas_height = number_of_rows*size + size*4
#call Tk()
userInterface = Tk()
stringGeometr = str(canvas_width) + "x" +str(canvas_height)+"+10+20"
#call geometry method
userInterface.geometry(stringGeometr)
a_canvas = Canvas(userInterface)
#set the background color as white for canvas
a_canvas.config(background="white")
#call pack method through canvas a_canvas
a_canvas.pack(fill='both', expand=True)
#call draw_pattern method
draw_pattern(a_canvas,colours_dictionary,pattern_list,size,left,top)
#call mainloop method
userInterface.mainloop()
#call main method
main()
***********************************************************************************************************************************
In case of any doubt do ask in the comment section