'Python, Turtle - File "<string>", line 12, in forward turtle.Terminator + Matplot lib code gets stuck after the graph is plot
Hi there I have written a program in Python 3.10 and i am using the Libraries Tuple and Matplotlib.pyplot as well as time.
I have 4 different classes: Polygon, Square(Polygon) - Child Class, Shape and Point.
The problems that I have are:
- After I plot some matplotlib graph I try to set a timer and close it but it gets stuck and I have to close it manually, what can I do about that ? SEE code below
class Point: """ Defining a point class a 2 dimensional point """
def __init__(self, x, y):
self.x = x
self.y = y
def plot(self):
fig = plt.scatter(self.x, self.y)
def __add__(self, other):
""" the other is a point type """
if isinstance(other, Point):
# this case is to add Points together
x = self.x + other.x
y = self.y + other.y
return Point(x, y)
else:
# this case is to add just numbers eg a + 5
x = self.x + other
y = self.y + other
return Point(x, y)
def main():
plt.show(block=False)
plt.show()
time.sleep(5)
plt.close()
Problem 2: I have made some classes and use Turtle to draw the shapes but when I draw one shape a pop up windows appears and then when i close it and then try to draw another one i get the following error:
File "", line 12, in forward
turtle.Terminator
class Shape():
""" """
def __init__(self, sides, name, angle1, angle2, colour, side_length=100, line_thickness=5, x_pos=90, y_pos=90):
# we added a default value to the parameters side_length and color
""" always when using init the first parameter we pass is self
and then we need to think what else is important to our class.
In this case we need to think what is important to a polygon.
For example sides and maybe we need to give it a name too.
"""
# The last step when initialising things is to define the parameters
# properties
self.sides = sides
self.name = name
self.side_length = side_length
self.colour = colour
self.line_thickness = line_thickness
self.x_pos = x_pos
self.y_pos = y_pos
self.angle1 = angle1
self.angle2 = angle2
def drawStar(self):
turtle.color('red')
turtle.begin_fill()
# stop writing - lift the pen up
turtle.penup()
# go to this position -
turtle.setpos(self.x_pos, self.y_pos)
# set the speed that turtle draws
turtle.speed(20)
for i in range(self.sides):
# start writing
turtle.pendown()
turtle.color(self.colour)
turtle.pensize(self.line_thickness)
turtle.forward(self.angle1)
turtle.left(self.angle2)
turtle.end_fill()
turtle.done()
def draw(self):
# stop writing - lift the pen up
turtle.penup()
# go to this position -
turtle.setpos(self.x_pos, self.y_pos)
for i in range(self.sides):
# start writing
turtle.pendown()
turtle.color(self.colour)
turtle.pensize(self.line_thickness)
turtle.forward(self.side_length)
# we can use the self parameter that we have passed into our class method to access the self.individual_angle
# that we initialised on the creation of our class to turn x degrees
turtle.right(180 - self.individual_angle)
3 I do not want to draw them on the same graph, ideally I would want to drawn them on different graphs if there is a way and also be able to close a graph but when I want to draw a new one i want to be able to. Sometimes I want to draw multiple things on 1 plot.
Ideally I want to be able to plot multiple graphs in matplotlib on different windows as well.
Am I doing something completely wrong or is there a way that I am not using at the moment? Would it be possible to fix the code ? add stuff?
This is not my whole code but if you want i can provide you more of my code.
star = Shape(50, 'Star', angle1=200, angle2=170, line_thickness=1, x_pos=-100, y_pos=0, colour='red')
star.drawStar()
plt.show(block=False)
plt.show()
time.sleep(5)
plt.close()
turtle.Screen()
x= Polygon(4, "4", 100, line_thickness=10, x_pos=-300, y_pos=-20)
y = Polygon(5, "5", colour="red", x_pos=-50, y_pos=-220)
z = Polygon(6, "6", 150, "orange", x_pos=180, y_pos=320)
z.draw()
y.draw()
x.draw()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
