Exiting the turtle screen



I am made this class to create points, determine the area of the figure, and draw the figure using the turtle module. I use Pycharm’s Python Console to run the class. I want to display the figure, exit, modify the list, display the figure, … but I get an error once I close the window by clicking the red x button and can’t display the new figure. How do I fix this? Also, is there any way to improve my code as a whole? I am not sure if I should place draw_figure() and calculate_area() in the class because they don’t have self as a parameter.


import turtle

class Coordinate:

    list_of_points = []
    number_of_points = 0

    def __init__(self, x, y):
        self.x = x
        self.y = y
        Coordinate.number_of_points += 1
        Coordinate.list_of_points.append([self.x, self.y])

    def remove(self):
        Coordinate.list_of_points.remove([self.x, self.y])

    def draw_figure():

        for point in Coordinate.list_of_points:
            turtle.dot(3, "red")


    def calculate_area():  # signed area
        if len(Coordinate.list_of_points) == 0:
            print("There are no points.")
        elif len(Coordinate.list_of_points) == 1:
            print("There is only one point.")

        else:  # shoelace formula
            area = 0
            for index in range(len(Coordinate.list_of_points) - 1):
                area += Coordinate.list_of_points[index][0] * Coordinate.list_of_points[index + 1][1]\
                    - Coordinate.list_of_points[index + 1][0] * Coordinate.list_of_points[index][1]

            area += Coordinate.list_of_points[-1][0] * Coordinate.list_of_points[0][1]\
                - Coordinate.list_of_points[0][0] * Coordinate.list_of_points[- 1][1]

            area = 0.5 * abs(area)

            print("Area of the figure: ", area)


maybe pycharm is using the main thread to watch your variables (perhaps it should be doing it the other way around)

the other error is probably from not cleanly resetting the state of turtle, perhaps there was some cleanup you were supposed to do before opening another window, you’d know better than me


Can you explain what the main thread is? I’m new to programming. Thanks


It’s the thread the program starts with. I’m guessing pycharm runs its own code and starts another thread for yours to run in.

I’d run the program without pycharm.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.