-1

I wanted to make a blinking "Press SPACE to start!" screen and if SPACE was pressed, the program should shut down but I always get the turtle.terminator error. Can someone help me please?

from turtle import *
import pygame
while True:
    x = 0
    penup()
    tracer(0)
    if x == 0:
        color("black")
        goto(-80,-170)
        write("Press SPACE to start!",font=50)
        update()
        pygame.time.delay(1*800)
        x = 1
    if x == 1:
        color("white")
        goto(-90,-180)
        begin_fill()
        goto(90,-180)
        goto(90,-140)
        goto(-90,-140)
        goto(-90,-180)
        end_fill()
        update()
        x = 0
        pygame.time.delay(1*800)
    def close():
        bye()
    onkey(close(),"Space")
done()

After integrating advice from the comments, it still raises an error when I use onkey(close,...) instead of onkey(close(),..).

ggorlen
  • 44,755
  • 7
  • 76
  • 106

2 Answers2

1

There are a few issues here. A word of advice: work in small bursts and run your code often to validate all of your assumptions at each step of the way. This code looks as if it was all written in one fell swoop, and once it didn't work, there was too much complexity to isolate the bugs. Had you run the code to test it often as you went along, you'd see those errors immediately and the solutions would be more obvious.

Try to minimize the problem space by breaking your functionality into small pieces and validating each one.

First, let me provide my solution:

import turtle


def render_white_screen():
    win.ontimer(render_press_space, delay_ms)
    t.clear()
    turtle.update()


def render_press_space():
    win.ontimer(render_white_screen, delay_ms)
    t.write(
        "Press SPACE to start!",
        move=False,
        align="center",
        font=("Arial", 20, "normal"),
    )
    turtle.update()


delay_ms = 800
turtle.tracer(0)
win = turtle.Screen()
win.onkey(turtle.bye, "space")
win.listen()
t = turtle.Turtle()
t.hideturtle()
t.color("black")
render_press_space()
turtle.mainloop()

Turtle is surprisingly not especially beginner-friendly and has a huge list of gotchas, so it's important to check the docs and do research on Stack Overflow as soon as you encounter an error.

Errors:

Suggestions/better practices:

  • Always use import turtle, never from turtle import *. The reason is that from turtle import * dumps 160+ functions into the global namespace, causing potential clashes with your own functions. Many of these functions have common names like update and reset that can easily cause confusion and bugs. Turtle has a module-level instance ostensibly to avoid confusion for beginners, but it only winds up introducing more problems in the long run than simply creating a turtle instance.
  • Don't import pygame just to sleep. Python has a native function sleep available in the time module. But even better is to use turtle's ontimer callback as shown above.
  • You can clear the screen with t.clear().
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Thank you for your advice! First i wanted to say, that this is just a cutout form a code that also why i imported Pygame because it i need it later in my code. My whole code works without the code that i posted in here. I did not know that "from xxx import *" can cause bugs, I always used this methode, because it is less to write. How do you use the t.clear(), does it clear the whole screen or can you give arguments? – trueShadoWrr Dec 17 '22 at 21:12
  • Yep, dumping the whole namespace into your scope is great, saving a couple characters worth of typing, until your program suddenly breaks and you spent 4-5 hours trying to figure out why. Even if you're lucky and avoid the bugs, prefixing all turtle calls with `turtle.` makes it much easier to see what belongs to the turtle library and what belongs to you. See the docs for [`clear()`](https://docs.python.org/3/library/turtle.html#turtle.clear)--there are no arguments. – ggorlen Dec 17 '22 at 21:17
  • Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\xxx\AppData\Local\Programs\Python\Python310-32\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "C:\xxxPython\Python310-32\lib\tkinter\__init__.py", line 839, in callit func(*args) TypeError: PSTS() missing 1 required positional argument: 'win' Traceback (most recent call last): File "c:\Users\xxx\xxx.py", line 126, in Intro() File "c:\Users\xxx.py", line 119, in Intro turtle.tracer(0) File "", line 5, in tracer turtle.Terminator – trueShadoWrr Dec 17 '22 at 21:48
  • Did you try running my code on its own first? There's probably an error elsewhere in your 120 lines of code. When you look at error tracebacks, you should look for the line that refers to your code, not the library's code where the error originated. It's probably line 119. I explained this error in my answer: terminator errors occur when you exit turtle, then try to execute more turtle functions. Debug by minimizing/isolating the problem: `import turtle; turtle.bye(); turtle.tracer()`. – ggorlen Dec 17 '22 at 21:51
  • t = turtle.Turtle() def PSTS(win): win.ontimer(WB(win), 800) t.color("black") t.write("Press SPACE to start!",move=False,align="center",font=("Arial",20,"normal"),) turtle.update() def WB(win): win.ontimer(PSTS, 800) t.color("white") t.goto(-110,-180) t.begin_fill() t.goto(110,-180) t.goto(110,-140) t.goto(-110,-140) t.goto(-110,-180) t.end_fill() turtle.update() while True: turtle.tracer(0) win = turtle.Screen() win.onkey(turtle.bye, "space") win.listen() PSTS(win) turtle.mainloop() – trueShadoWrr Dec 17 '22 at 21:56
  • Please ask a new question. I can't understand that. Don't use `while True`, just timers, as my code shows. – ggorlen Dec 17 '22 at 21:57
  • This code alone does not work either. Other question, what should I do if I want to response to your comment but my message is too long? Split it in 2 seperat comments? – trueShadoWrr Dec 17 '22 at 21:58
  • The point of SO is to ask a clear enough question focused on one specific problem such that comments and longwinded back-and-forth discussions aren't necessary. I believe I've pointed out all of the problems in your snippet and provided a working solution. It's up to you to adapt it to your larger codebase to the extent you want. If my solution doesn't work if you run it as a separate program, what error are you encountering exactly, and what environment/version are you running it in? It runs fine for me, so without this information I can't assist further. – ggorlen Dec 17 '22 at 22:00
  • I use python 3.10.2 because first I had problems installing pygame and when I used this version, it worked. I don't really know what error I am encountering I am very new to programming so I don't have that much knowledge. You said I should ask a new Question, you mean a new "Question in SO? – trueShadoWrr Dec 17 '22 at 22:04
  • Sorry--did my code work for you or not? I don't understand the issue. Yes, I'm suggesting a new question on Stack Overflow with your new problem as a [mcve]. The problems here are a terminator error and some confusion with key handlers. That's been cleared up on this small excerpt from your larger codebase. If you have a larger codebase you're struggling with, those are new and different problems that belong in new threads, each with a clear, specific, minimal question focused on a small chunk of code that removes unnecessary distractions. – ggorlen Dec 17 '22 at 22:05
  • Your code works fine for me. Basicly I used your code and modified it just a bit not much for example the clear screen, because I don't want to clear the whole screen just a "rectangle" for the blinking effect on the "Press SPACE to start! So now I guess I found the problem: I handled the win over to the function which you did not, because I thought I have to do it but it is wrong. It is my fault because I did not know this sorry! But now I am happy that my code works (i guess) and I wanted to thank you again for your help! Have a great day! – trueShadoWrr Dec 17 '22 at 22:08
0

It is "space", not "Space". Also, it is still correct to use onkey(close, ...).

The complete function would be onkey(close, "space")

MrDiamond
  • 958
  • 3
  • 17