Question: import turtle as t import threading def save _ drawing ( x _ list, y _ list, file _ name ) : with open (

import turtle as t
import threading
def save_drawing(x_list, y_list, file_name):
with open(file_name, "w") as file:
for x, y in zip(x_list, y_list):
file.write(f"{x},{y}
")
def stop_drawing(x, y):
global continue_drawing
continue_drawing = False
def handle_click(x, y):
global points
points.append((x, y))
t.goto(x, y)
t.dot()
def main():
global points, continue_drawing
t.speed(0)
t.hideturtle()
fillcolor = t.textinput('Input', 'Enter fill color:')
t.fillcolor(fillcolor)
bgcolor = t.textinput('Input', 'Enter background color:')
t.Screen().bgcolor(bgcolor)
# Initial drawing
points =[]
t.onscreenclick(handle_click, 1) # Left-click
t.onscreenclick(finalize_shape, 3) # Right-click
t.onscreenclick(stop_drawing, 2) # Middle-click (scroll wheel click)
t.penup()
t.goto(-200,200)
t.write("Initial Drawing: Left-click to add points, right-click to finish, middle-click to stop", font=("Arial",12, "normal"))
t.mainloop()
# Allow user to draw additional shapes
continue_drawing = True
while continue_drawing:
t.reset() # Reset the screen before drawing a new shape
points =[]
# Instructions
t.onscreenclick(handle_click, 1) # Left-click
t.onscreenclick(finalize_shape, 3) # Right-click
t.onscreenclick(stop_drawing, 2) # Middle-click (scroll wheel click)
t.penup()
t.goto(-200,200)
t.write("Left-click to add points, right-click to finish, middle-click to stop", font=("Arial",12, "normal"))
if not continue_drawing:
filename = t.textinput("Save Drawing", "Enter filename to save the drawing:")
if filename:
save_drawing(filename, points)
def finalize_shape(x, y):
global continue_drawing
if len(points)>1:
t.penup()
t.goto(x, y)
choice = t.textinput("Choose", "Straight (S) or Curve (C)?(Enter to continue drawing, Q to quit)").upper()
if choice =="S":
draw_polygon(points) if len(points)>2 else t.goto(points[0])
elif choice =="C":
draw_curve(points)
elif choice =="Q":
global continue_drawing
continue_drawing = False
t.penup()
points.clear()
def toggle_pen(x, y):
if t.isdown():
t.penup()
else:
t.pendown()
def draw_polygon(points):
t.penup()
t.begin_fill()
t.goto(points[0])
t.pendown()
for point in points[1:]:
t.goto(point)
t.goto(points[0])
t.end_fill()
def draw_
curve(points):
t.begin_fill()
for i in range(0, len(points)-3,3):
P =[points[i], points[i +1], points[i +2], points[i +3]]
for o in range(101):
x = bezier(o /100, P[0][0], P[1][0], P[2][0], P[3][0])
y = bezier(o /100, P[0][1], P[1][1], P[2][1], P[3][1])
t.goto(x, y)
if o ==0:
t.pendown()
t.end_fill()
def bezier(o, p0, p1, p2, p3):
return (1- o)**3* p0+3*(1- o)**2* o * p1+3*(1- o)* o **2* p2+ o **3* p3
if __name__=="__main__":
main()
def load_drawing(file_name):
x_list =[]
y_list =[]
try:
with open(file_name, "r") as file:
for line in file:
x, y = map(float, line.strip().split(','))
x_list.append(x)
y_list.append(y)
except FileNotFoundError:
print("File not found.")
return x_list, y_list
this program is stuck on a loop and I cannot find a way for it stop drawing and save the coordinates of the places clicked on turtle screen into a text file
help me make this program be able to save and load drawings

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!