Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from tkinter import * import random #------------------------------------------- #------------------------------------------- # 1) Make one change to the main() function #------------------------------------------- def main(): size = 10 number_of_rows =

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

from tkinter import * import random

#------------------------------------------- #------------------------------------------- # 1) Make one change to the main() function #------------------------------------------- def main(): size = 10 number_of_rows = 50 max_in_each_row = 40 min_in_each_row = 25 start_left1 = size * 2 start_down = size * 2

canvas_width = size * max_in_each_row * 2 +size * 5 canvas_height = number_of_rows * size + size * 4 window = Tk() window.title("A5 by") geometry_string = str(canvas_width)+"x"+str(canvas_height)+"+10+20" window.geometry(geometry_string) a_canvas = Canvas(window)

a_canvas.config(background="blue") a_canvas.pack(fill=BOTH, expand = True) #Canvas fills the whole window

pattern_list = get_list_of_rows(number_of_rows, min_in_each_row, max_in_each_row) #2

longest_string_length = get_length_of_longest(pattern_list) #3 print_pattern_info(pattern_list, longest_string_length, size) #4

draw_pattern_version1(a_canvas, pattern_list, size, start_left1, start_down)#5

start_left2 = start_left1 + size * 3 + longest_string_length * size draw_pattern_version2(a_canvas, pattern_list, size, start_left2, start_down, longest_string_length) #6

window.mainloop() #------------------------------------------- #------------------------------------------- # 2. get_list_of_rows() function #------------------------------------------- def get_list_of_rows(number_of_rows, min_in_each_row, max_in_each_row): possible_digits = "12345" #------------------------------------------- #------------------------------------------- # 3. get_length_of_longest() function #------------------------------------------- def get_length_of_longest(pattern_list): return 0 #------------------------------------------- #------------------------------------------- # 4. print_pattern_info() function #------------------------------------------- def print_pattern_info(pattern_list, longest_length, size): pass #------------------------------------------- #------------------------------------------- # 5. draw_pattern_version1() function #------------------------------------------- def draw_pattern_version1(a_canvas, pattern_list, size, left, top): possible_digits = "12345" colours = ["red", "blue", "green", "yellow", "black"] #------------------------------------------- #------------------------------------------- # 6. draw_pattern_version2() function #------------------------------------------- def draw_pattern_version2(a_canvas, pattern_list, size, left, top, longest_width): possible_digits = "12345" colours = ["red", "blue", "green", "yellow", "black"]

main()

Assignment 5 (35 marks) This assignment creates a list of strings where each element has a random length and is made up of random digits. On the left hand side of the Canvas area the program draws a grid of coloured rectangles where each row corresponds to each list element and each rectangle along the row corresponds to each digit of the list element. On the right hand side of the Canvas area the program draws a mirror image of the grid of coloured rectangles. 1. Add your username to the title bar of the window Currently the title bar of the program window displays "A5 by". Add your username to the title bar of the program, i.e., the title bar should display the string, "A5 by yourusername", e.g., A5 by afer023. 2. get_list_of rows(number_of_rows, min_in_each_row, max_in_each_row) This function returns a list of strings. Each element of the list is made up of random digits (1, 2, 3, 4 or 5). The function is passed three integer parameters: The length of the list which is returned by the function, The minimum length of each string element of the list which is returned by the function, The maximum length of each string element of the list which is returned by the function, The length of each string element of the list is a random number between the second and third parameter values (both inclusive) For example, a possible output produced by the following code is shown in the text box: print("1.", get_ list_of_rows(3, 5, 10)) print("2.", get list of rows (2, 3. 7)) 1 '234344415', 34414232434523'] 2. '2214545', 1421 3. get length_of_longest(pattern_list) This function is passed a list of strings as a parameter. The function returns the length of the longest element of the parameter list. For example, the output produced by the following code is shown in the text box on the right: print("1.", get length of longest ([' 234344415, '3441423','2434523']))1. 9 print("2.", get length of longest ([ ' 2214545', '142'])) 2. 7 4. print pattern_info(pattern_list, longest_length, size) This function is passed three parameters: a list of strings and two integers. The function prints the parameter information in the following format: Line 1: the string "Pattern info:", Line 2: the string "Size: n pixels" where n is the size parameter value, Line 3: the string "Number of rows: "followed by the length of the pattern_list parameter, followed by the string ", longest row length: " followed by the longest_length parameter, Line 4: a blank line, Line 5: the string "Pattern of digits in each row: The rest of the output is a numbered list (starting from the number 1) of all the elements of the parameter list. Each line of this numbered list is preceded by 4 spaces and there is a " - " between the line number and the element. For example, the output produced by the following code is shown in the text box: print_pattern_info(I '234344415', '3441423'. 2434523' 1, 9, 10) Pattern info: Size: 10 pixels Number of rows: 3, longest row length: 9 Pattern of digits in each row: 1234344415 2 3441423 3 -243452 5. draw_pattern_version1(a_canvas, pattern_list, size, left, top) This function is passed five parameters: the Canvas object, a list of string elements, 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. Each element of the pattern_list parameter is a string of digits and each element corresponds to one row of coloured rectangles. Each digit of the string element dictates the colour of the rectangle, e.g., the digit 1 means that the rectangle is filled with red, the digit 2 means that the rectangle is filled with blue, the digit 3 means that the rectangle is filled with green, etc. Note that the first two lines of this function are (you may use any colours of your choice): possible digits "12345" colours["red", "blue", "green", "yellow", "black" Once you have completed this function you should see rows of coloured rectangles (of varying lengths) on the left hand side of the canvas area. . 6. draw_pattern_version2(a_canvas, pattern_list, size, left, top, longest_width) This function is passed six parameters: the Canvas object, a list of string elements, followed by four integer parameters. Inside the canvas area the function draws a mirror image of the grid of coloured rectangles drawn in Part 5. above, i.e., if you were to fold the canvas down the middle line each coloured square lies exactly over a square of the same colour. Note that the first two lines of this function are (use the same colours as you used in Part 6. above): possible digits - "12345 colours- ["red", "blue", "green", "yellow", "black"] Once you have completed this function and you test your program, you should see a mirror image of the Part 5. grid of coloured squares on the right hand side of the Canvas area. The program skeleton: The skeleton of this assignment program is shown below. You need to define five of the function:s and make one small change to the main() function. Rename the file to 'YourUsernameA5-py', e.g., afer023A5.py

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

Would giving rewards or administering punishments be

Answered: 1 week ago