Python Program Help: I need to create a program that will ask the user to enter
ID: 673094 • Letter: P
Question
Python Program Help: I need to create a program that will ask the user to enter file name (only the ones I list below) and read the input text files(I list below)
and draw in turtle accordingly. Certain functions need to be created. After finishing drawing one file, program will ask the user to enter another name.
The input files are the followinging(Please copy and past to a notepad in order for the python to read):
(1)"Circle.turtle" file:
color blue
circle -50 50 100
color green
circle 50 -50 100
color red
circle 0 0 100
(2)"star" file:
color blue
line -200 200 0 400
color red
line 200 200 -144 400
color green
line -124 -35 72 400
color black
line 0 345 288 400
color purple
line 124 -35 144 400
Explanation / Answer
from Tkinter import *
def draw_all(file):
colors = ["red"]
master = Tk()
canvas_width = 1000
canvas_height = 800
w = Canvas(master, width=canvas_width,height=canvas_height)
for line in file:
if ("color" in line):
colors[0] = line.split(" ")[1][:len(line.split(" ")[1])-1]
else:
l = line.split(' ')
if (l[0] == "circle"):
w.create_oval(int(l[1]) - int(l[3]), int(l[2]) - int(l[3]), int(l[1]) + int(l[3]), int(l[2]) + int(l[3]), fill=colors[0], width=2)
else:
w.create_line(int(l[1]),int(l[2]),int(l[3]),int(l[4]),fill = colors[0],width = 2)
w.pack(fill=BOTH, expand=1)
mainloop()
def main():
print 'Enter File Name ',
file_name = raw_input()
file = open(file_name,'r')
draw_all(file)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.