Search code examples
pythonpython-3.xdrawingturtle-graphicspython-turtle

Tie circle radius to window size in Python Turtle


In Python turtle, when drawing objects on the screen, if there was a way to have a circle's radius connected to the window width or height, so that it can be resized by altering the window size?


Solution

  • Yes, it is possible.

    You will need to create an event that the turtle will listen to: In the following example, if you click on the turtle, a circle of half the width of the canvas will be drawn.
    If you resize the canvas, and click on the turtle again, a new circle of half the new width will be redrawn.

    import turtle
    
    
    def start(dummy_a, dummy_b):
        t.reset()
        y, x = screen.window_height(), screen.window_width()
        t.home()
        t.circle(x/4)
    
    
    if __name__ == '__main__':
    
        screen = turtle.Screen()
        t = turtle.Turtle()
    
        t.onclick(start, add=True)
        screen.listen()
        turtle.done()