Question
What code should I use for # draw_pattern() function part? I should use text file given below colours.txt tree.txt black white red orange yellow green
What code should I use for # draw_pattern() function part?
I should use text file given below
colours.txt | tree.txt |
black white red orange yellow green cyan blue brown teal | 11155111 11155111 11555511 11555511 15555551 55555555 11188111 11188111 |
My code---------------------------------
""" Nakyeng Lee, nlee127 This program is for assignment 5 creating pattern text file """
from tkinter import *
#------------------------------------------- # main() function #------------------------------------------- def main(): size = 10 start_left = size * 2 start_down = size * 2 colours_filename = input("Enter a filename (colours): ") colours = read_colours(colours_filename) filename = input("Enter a filename: ") pattern_list = read_pattern(filename) number_of_rows = len(pattern_list) number_of_columns = len(pattern_list[0]) canvas_width = size * number_of_columns +size * 4 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="white") a_canvas.pack(fill=BOTH, expand = True) #Canvas fills the whole window draw_pattern(a_canvas, pattern_list, colours, size, start_left, start_down) window.mainloop()
#------------------------------------------- # read_colours() function #------------------------------------------- def read_colours(filename): file = open(filename, 'r') file_content = file.read() file.close() list_of_colours = file_content.split(" ")
list_of_colours = list(list_of_colours)
return list_of_colours
#------------------------------------------- # split_digits() function #------------------------------------------- def split_digits(line): digits = [] for i in line: digits.append(i) return digits
#------------------------------------------- # process_file() function #------------------------------------------- def process_file(filename): lines=[] file = open(filename) for line in file: lines.append(line.strip()) return lines
#------------------------------------------- # read_pattern() function #------------------------------------------- def read_pattern(filename): list_of_digits = [] list_of_strings = process_file(filename) for s in list_of_strings: list_of_digits.append(split_digits(s)) return list_of_digits
#------------------------------------------- # draw_pattern() function #------------------------------------------- def draw_pattern(a_canvas, pattern_list, colours, size, left, top): #modify this part
main()
This assignment first reads a list of colours from a text file and creates a list of colour strings. For example, the colours.txt contains the following lines: black white red and returns a list of strings: ("black", "white", "red", "orange", "yellow", "green", "cyan", "blue", "brown", "teal" ] Next, your program reads lines of digits from a pattern text file and draws a grid of coloured rectangles based on the information stored in the text file. Each row of the picture corresponds to a line in the file, and each rectangle along the row corresponds to a digit in the line. For example, if the colour codes that we use are 0: black, 1: white, 2: red, 3: orange, 4: yellow, 5: green, 6: cyan, 7: blue, 8: brown and 9: teal, the following table shows the contents of the pattern file, "tree.txt" and the corresponding pixel art. The first row (11155111) represents 3 white rectangles, two green rectangles and three white rectangles. Pixel Art tree.txt (simple mode) 11155111 11155111 115 511 Puoi un un PUU unun Cu un o on P 3. Complete the read colours (filename) function (3 marks) This function takes a filename as a parameter and returns a list of strings read from the input file. 4. Complete the split digits (line) function (3 marks) This function takes a String of digits as a parameter and returns a list of String elements. Each element of the list is made up of a digit (0, 1, 2, 3, 4, 5, 6, 7, 8 or 9). For example, the output produced by the following call to the function is shown below: print("1.", split_digits ('11155111')) 1. [1, 1, 1, 5, 5, 1, 1, 1] 5. Complete the process_file(filename) function (5 marks) This function takes a filename as a parameter and returns a list of strings read from the input file. For example, the output produced by the following call to the function is shown below: lines = process file ("tree.txt") print ("2.", lines) 2. ['11155111', '11155111', '11555511', '11555511', '15555551', '55555555', 111881111 111881111 6. Complete the read_pattern (filename) function (5 marks) This function takes a filename as a parameter. The function calls the process_file() defined above to read a pattern from a text file then returns a list of lists. Each element of the list is a list of digits. The list of digits is produced by calling the split_digits () function above. For example, the output produced by the following call to the function is shown below: pattern_list = read pattern ("tree.txt") print("3.", pattern_list) 3. [[1, 1, 1, 5, 5, 1, 1, 1], [1, 1, 1, 5, 5, 1, 1, 1], [1, 1, 5, 5, 5, 5, 1, 1], [1, 1, 5, 5, 5, 5, 1, 1], [1, 5, 5, 5, 5, 5, 5, 1], [5, 5, 5, 5, 5, 5, 5, 5), (1, 1, 1, 8, 8, 1, 1, 1], [1, 1, 1, 8, 8, 1, 1, 1]] 7. Complete the draw pattern (a canvas, pattern list, colours, size, left, top) (5 marks) This function is passed SIX parameters: the Canvas object, a list of string elements, a list of string (colours) 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. This assignment first reads a list of colours from a text file and creates a list of colour strings. For example, the colours.txt contains the following lines: black white red and returns a list of strings: ("black", "white", "red", "orange", "yellow", "green", "cyan", "blue", "brown", "teal" ] Next, your program reads lines of digits from a pattern text file and draws a grid of coloured rectangles based on the information stored in the text file. Each row of the picture corresponds to a line in the file, and each rectangle along the row corresponds to a digit in the line. For example, if the colour codes that we use are 0: black, 1: white, 2: red, 3: orange, 4: yellow, 5: green, 6: cyan, 7: blue, 8: brown and 9: teal, the following table shows the contents of the pattern file, "tree.txt" and the corresponding pixel art. The first row (11155111) represents 3 white rectangles, two green rectangles and three white rectangles. Pixel Art tree.txt (simple mode) 11155111 11155111 115 511 Puoi un un PUU unun Cu un o on P 3. Complete the read colours (filename) function (3 marks) This function takes a filename as a parameter and returns a list of strings read from the input file. 4. Complete the split digits (line) function (3 marks) This function takes a String of digits as a parameter and returns a list of String elements. Each element of the list is made up of a digit (0, 1, 2, 3, 4, 5, 6, 7, 8 or 9). For example, the output produced by the following call to the function is shown below: print("1.", split_digits ('11155111')) 1. [1, 1, 1, 5, 5, 1, 1, 1] 5. Complete the process_file(filename) function (5 marks) This function takes a filename as a parameter and returns a list of strings read from the input file. For example, the output produced by the following call to the function is shown below: lines = process file ("tree.txt") print ("2.", lines) 2. ['11155111', '11155111', '11555511', '11555511', '15555551', '55555555', 111881111 111881111 6. Complete the read_pattern (filename) function (5 marks) This function takes a filename as a parameter. The function calls the process_file() defined above to read a pattern from a text file then returns a list of lists. Each element of the list is a list of digits. The list of digits is produced by calling the split_digits () function above. For example, the output produced by the following call to the function is shown below: pattern_list = read pattern ("tree.txt") print("3.", pattern_list) 3. [[1, 1, 1, 5, 5, 1, 1, 1], [1, 1, 1, 5, 5, 1, 1, 1], [1, 1, 5, 5, 5, 5, 1, 1], [1, 1, 5, 5, 5, 5, 1, 1], [1, 5, 5, 5, 5, 5, 5, 1], [5, 5, 5, 5, 5, 5, 5, 5), (1, 1, 1, 8, 8, 1, 1, 1], [1, 1, 1, 8, 8, 1, 1, 1]] 7. Complete the draw pattern (a canvas, pattern list, colours, size, left, top) (5 marks) This function is passed SIX parameters: the Canvas object, a list of string elements, a list of string (colours) 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 areaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started