-2

I writed some code from a python book and the book shows that if you run the code there will be no problem

but when I ran the code there was a problem

This is my code:

import turtle
import time

turtle.pensize(5)
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.turtlesize(2,2,2)

def ileri():
    turtle.fd(5)
def geri():
    turtle.backward(5)
def sag():
    turtle.right(90)
    turtle.fd(5)
    turtle.left(90)
def sol():
    turtle.left(90)
    turtle.fd(5)
    turtle.right(90)
def sagdon():
    turtle.right(10)
def soldon():
    turtle.left(10)
def siyah():
    turtle.pencolor("black")
def yesil():
    turtle.pencolor("green")
def acikyesil():
    turtle.pencolor("lightgreen")
def mavi():
    turtle.pencolor("blue")
def acikmavi():
    turtle.pencolor("lightblue")
def sari():
    turtle.pencolor("yellow")
def kirmizigul():
    turtle.pencolor("red")

turtle.onkeypress(ileri, "w" or "Up")
turtle.onkeypress(geri, "s" or "Down")
turtle.onkeypress(sag, "d")
turtle.onkeypress(sol, "a")
turtle.onkeypress(sagdon, "Right")
turtle.onkeypress(soldon, "Left")
turtle.onkeypress(siyah, "0")
turtle.onkeypress(yesil, "1")
turtle.onkeypress(acikyesil, "2")
turtle.onkeypress(mavi, "3")
turtle.onkeypress(acikmavi, "4")
turtle.onkeypress(sari, "5")
turtle.onkeypress(kirmizigul, "6")
turtle.listen()

when I try to run this game every hecking time a screen opens for like 0.1 second and closes

if you try to help it will be so good for me

I was expecting the screen was going to be remain open because it was in the books image but I was not expecting to see the screen for just 0.1 seconds

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Eral
  • 3
  • 1
  • Does this answer your question? [Python Turtle Graphics Window only Opens Briefly then Closes](https://stackoverflow.com/questions/19018243/python-turtle-graphics-window-only-opens-briefly-then-closes) – ggorlen Feb 12 '23 at 17:00

3 Answers3

1

Use turtle.done() at the end of your code and the window will stay until you close it yourself.

1

The official python wiki page for turtle clearly states that:

(turtle.mainloop) Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics.

Since your program lacks a call to the mainloop function, it closes as soon as it is runs. All you have to do is add a

turtle.mainloop()
turtle.done()

at the end of your program.

I hope this helps :)

  • It works but this time it doesn't detect my keybord. But thank you so much for your help – Eral Feb 13 '23 at 09:29
0

i believe this is what you tried to do all you had to do is import screen and set the screen setup and update it:

import turtle
from turtle import  Screen
import time

turtle.pensize(5)
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.turtlesize(2,2,2)

screen= Screen()
screen.setup(600,600)

def ileri():
    turtle.fd(5)
def geri():
    turtle.backward(5)
def sag():
    turtle.right(90)
    turtle.fd(5)
    turtle.left(90)
def sol():
    turtle.left(90)
    turtle.fd(5)
    turtle.right(90)
def sagdon():
    turtle.right(10)
def soldon():
    turtle.left(10)
def siyah():
    turtle.pencolor("black")
def yesil():
    turtle.pencolor("green")
def acikyesil():
    turtle.pencolor("lightgreen")
def mavi():
    turtle.pencolor("blue")
def acikmavi():
    turtle.pencolor("lightblue")
def sari():
    turtle.pencolor("yellow")
def kirmizigul():
    turtle.pencolor("red")
while(True):
    screen.update()
    time.sleep(0.07)
    turtle.onkeypress(ileri, "w" or "Up")
    turtle.onkeypress(geri, "s" or "Down")
    turtle.onkeypress(sag, "d")
    turtle.onkeypress(sol, "a")
    turtle.onkeypress(sagdon, "Right")
    turtle.onkeypress(soldon, "Left")
    turtle.onkeypress(siyah, "0")
    turtle.onkeypress(yesil, "1")
    turtle.onkeypress(acikyesil, "2")
    turtle.onkeypress(mavi, "3")
    turtle.onkeypress(acikmavi, "4")
    turtle.onkeypress(sari, "5")
    turtle.onkeypress(kirmizigul, "6")
    turtle.listen()
    screen.exitonclick()

hope it was usefull to you and goodluck.

amine0395
  • 16
  • 3