Python 为什么海龟窗口停止响应?
我试图将turtle模块用于python,但在第一次成功编译之后,它在进一步编译中停止响应。我在Spyder和Jupyter都试过,但结果是一样的 编辑1:这是我用过的代码,不是自制的,我是从youtube上学来的 编辑2:内核在我关闭turtle窗口后一直处于死亡状态,因为它没有响应Python 为什么海龟窗口停止响应?,python,python-3.x,anaconda,python-turtle,Python,Python 3.x,Anaconda,Python Turtle,我试图将turtle模块用于python,但在第一次成功编译之后,它在进一步编译中停止响应。我在Spyder和Jupyter都试过,但结果是一样的 编辑1:这是我用过的代码,不是自制的,我是从youtube上学来的 编辑2:内核在我关闭turtle窗口后一直处于死亡状态,因为它没有响应 import turtle wn=turtle.Screen() wn.title("Made by ") wn.bgcolor("black") wn.setup(wi
import turtle
wn=turtle.Screen()
wn.title("Made by ")
wn.bgcolor("black")
wn.setup(width=800,height=600)
wn.tracer(0) #Keeps the screen up
#Paddle A
paddle_a=turtle.Turtle() #object of turtle
paddle_a.speed(0) #speed of animation
paddle_a.shape("square")
paddle_a.color("red")
paddle_a.shapesize(stretch_wid=5, stretch_len=1) #default size is 20*20
paddle_a.penup() #not to draw lines
paddle_a.goto(-350,0)
#Paddle B
paddle_b=turtle.Turtle() #object of turtle
paddle_b.speed(0) #speed of animation
paddle_b.shape("square")
paddle_b.color("blue")
paddle_b.shapesize(stretch_wid=5, stretch_len=1) #default size is 20*20
paddle_b.penup() #not to draw lines
paddle_b.goto(350,0)
#Ball
ball=turtle.Turtle() #object of turtle
ball.speed(0) #speed of animation
ball.shape("circle")
ball.color("white")
ball.penup() #not to draw lines
ball.goto(0,0)
ball.dx=0.09 #moves by 2 pixels
ball.dy=0.09
#Function
def paddle_a_up():
y=paddle_a.ycor() #mentions the y coordinate
y=y+20
paddle_a.sety(y) #set y coordinate to new y
def paddle_a_down():
y=paddle_a.ycor() #mentions the y coordinate
y=y-20
paddle_a.sety(y) #set y coordinate to new y
def paddle_b_up():
y=paddle_b.ycor() #mentions the y coordinate
y=y+20
paddle_b.sety(y) #set y coordinate to new y
def paddle_b_down():
y=paddle_b.ycor() #mentions the y coordinate
y=y-20
paddle_b.sety(y) #set y coordinate to new y
#Keyboard binding
wn.listen() #listen keyboard input
wn.onkeypress(paddle_a_up,'w')
wn.onkeypress(paddle_a_down,'s')
wn.onkeypress(paddle_b_up,"Up")
wn.onkeypress(paddle_b_down,"Down")
#Main loop
running=True
while running:
wn.update() #updates the screen
#movement the ball
ball.setx(ball.xcor()+ball.dx)
ball.sety(ball.ycor()+ball.dy)
这是我使用的代码,不是我自己做的,我是从youtube上学到的
如果上传到YouTube上的人能够正确地学习海龟,这会有所帮助。此代码的主循环可能会阻止tkinter事件。代码也做了比必要的更多的工作,没有将海龟导向它的优势。看看我的重写是否更适合您:
from turtle import Screen, Turtle
# Functions
def paddle_a_up():
paddle_a.forward(20)
def paddle_a_down():
paddle_a.backward(20)
def paddle_b_up():
paddle_b.forward(20)
def paddle_b_down():
paddle_b.backward(20)
# Main loop
def move():
x, y = ball.position()
ball.setposition(x + ball.dx, y + ball.dy)
screen.update()
screen.ontimer(move)
screen = Screen()
screen.bgcolor('black')
screen.setup(width=800, height=600)
screen.tracer(False)
# Paddle A
paddle_a = Turtle()
paddle_a.shape('square')
paddle_a.shapesize(stretch_wid=1, stretch_len=5)
paddle_a.setheading(90)
paddle_a.penup()
paddle_a.color('red')
paddle_a.setx(-350)
# Paddle B
paddle_b = paddle_a.clone()
paddle_b.color('blue')
paddle_b.setx(350)
# Ball
ball = Turtle()
ball.shape('circle')
ball.color('white')
ball.penup()
ball.dx = 1 # user defined object properties
ball.dy = 1
# Keyboard binding
screen.onkeypress(paddle_a_up, 'w')
screen.onkeypress(paddle_a_down, 's')
screen.onkeypress(paddle_b_up, 'Up')
screen.onkeypress(paddle_b_down, 'Down')
screen.listen()
move()
screen.mainloop()
在Spyder和/或Jupyter下运行会在重新启动turtle程序时增加更多的复杂性。以上内容仅在命令行中测试。您需要显示代码;可能会有很多不同的问题。显示你的代码,当有很多可能导致这种情况的问题时,很难提供帮助。