Python龟坚持输入和程序一直没有响应

时间:2018-04-14 07:45:08

标签: python

我正在尝试在python中运行一个简单的代码,尝试在MS Visual Studio,Pycharm,Eclipse上运行。按下按钮后,应用程序打开并无响应。任何的想法?我在Windows 10 64上运行。我错过了什么吗?

import os
import turtle

wn=turtle.Screen()
wn.bgcolor("black")
wn.title("primul")


border_pen=turtle.Turtle()
border_pen.speed(0)
border_pen.color("blue")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
border_pen.hideturtle()

player=turtle.Turtle()
player.color("red")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)

playerspeed=15
def move_left():
    x=player.xcor()
    x-=playerspeed
    player.setx(x)

turtle.listen()
turtle.onkey(move_left,"Left")


delay=input("press enter")

1 个答案:

答案 0 :(得分:0)

代码问题:  您正在等待input('press enter'),这将停止GUI框架。您需要将其替换为wn.mainloop(),这会阻止您退出屏幕。

import os
import turtle
import tkinter

wn=turtle.Screen()
wn.bgcolor("black")
wn.title("primul")


border_pen=turtle.Turtle()
border_pen.speed(0)
border_pen.color("blue")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
border_pen.hideturtle()

player=turtle.Turtle()
player.color("red")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)

playerspeed=15
def move_left():
    x=player.xcor()
    x-=playerspeed
    player.setx(x)

turtle.listen()
turtle.onkey(move_left,"Left")

wn.mainloop()