How to close the Python turtle window after it does its code?

turtle.bye(), aka turtle.Screen().bye(), closes a turtle graphics window. Usually, a lack of turtle.mainloop(), or one of its variants, will cause the window to close because the program will exit, closing everything. turtle.mainloop() should be the last statement executed in a turtle graphics program unless the script is run from within Python IDLE -n which disables … Read more

Python Turtle.Terminator even after using exitonclick()

Your turtle program is structured incorrectly. You needn’t do: tw = t.Screen() … tw.exitonclick() in every function. Screen() only needs to be called once; exitonclick() should only ever be called once. Try this restructuring: import turtle as t def square(): for i in range(4): t.forward(100) t.right(90) def triangle(): for i in range(3): t.forward(100) t.right(120) def … Read more

How to create a subclass in python that is inherited from turtle Module

Rounding up Ignacio’s answer and orokusaki’s comment you probably should write something like import turtle class TurtleGTX(turtle.Turtle): “””My own version of turtle””” def __init__(self,*args,**kwargs): super(TurtleGTX,self).__init__(*args,**kwargs) print(“Time for my GTX turtle!”) my_turtle = TurtleGTX() my_turtle.forward(100)

AttributeError: partially initialized module ‘turtle’ has no attribute ‘Turtle’ (most likely due to a circular import)

You’ve made a common error that I happended to also make when I was investigating your question. I assume you have your code written in a file called ‘turtle.py‘? When you import turtle, it imports your file, not the turtle library. Rename your file to something other than turtle.py, and your code should run fine. … Read more

Turtle module – Saving an image

from tkinter import * # Python 3 #from Tkinter import * # Python 2 import turtle turtle.forward(100) ts = turtle.getscreen() ts.getcanvas().postscript(file=”duck.eps”) This will help you; I had the same problem, I Googled it, but solved it by reading the source of the turtle module. The canvas (tkinter) object has the postscript function; you can use … Read more

How to bind several key presses together in turtle graphics?

I’m skeptical that you can cleanly solve this coordinating variables between onkeypress() and onkeyrelease() events. (Though I’d be pleased to be shown otherwise.) I offer an alternate approach where key presses simply post move requests and a timer applies those requests, whether individual or doubled up: from turtle import Turtle, Screen win = Screen() flynn … Read more

Detecting collision in Python turtle game

This code seems to be more wishful thinking than actual programming: def is_collided_with(self, run): return self.rect.colliderect(run.rect) runner = run(10, 10, ‘my_run’) follower = follow(20, 10) if follow.is_collided_with(run): print ‘collision!’ Turtles don’t have a .rect() method. You can’t simply add a is_collided_with() method to an existing class with this def statement. There are no run() and … Read more