【问题标题】:Python tkinter and turtle errors after program stops程序停止后的 Python tkinter 和海龟错误
【发布时间】:2018-10-13 00:58:34
【问题描述】:

很抱歉打扰任何人,但是当我运行我的代码(我第一次尝试真正的游戏)时,一切正常,但是在我关闭它之后,打印了一些错误,我找不到其他人遇到我的问题。我的文件名为 Space Invaders.py,我使用的是 Pycharm(IDLE 也会出现这些错误)。这是我的代码:

import turtle
import math

print("------------Space Invaders - Python------------")
print("-------------GAME NOT YET COMPLETED------------")
print("This console is simply a status readout.")

wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")


borderPen = turtle.Turtle()
borderPen.speed(0)
borderPen.color("white")
borderPen.penup()
borderPen.setposition(-400, -400)
borderPen.pendown()
borderPen.pensize(3)
for i in range(4):
    borderPen.fd(800)
    borderPen.lt(90)
borderPen.hideturtle()


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


enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
enemy.setposition(-300, 250)

movementStepE = 2
movementStepEY = -15
movementStepP = 5 


def move_left():
    x = player.xcor()
    new_x = x - movementStepP
    if new_x < -380:
        new_x = -380
    player.setx(new_x)


def move_right():
    x = player.xcor()
    new_x = x + movementStepP
    if new_x > 380:
        new_x = 380
    player.setx(new_x)


def distancepyth(x1, x2, y1, y2):
    pyth = math.sqrt((x1 - x2) ** 2) + (y1 - y2 ** 2)
    return(pyth)


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

turtle.onkeypress(move_right, "Right")

new_y = 250  # 250
while True:

    x = enemy.xcor()
    y = enemy.ycor()
    new_x = x + movementStepE
    if new_x > 380:
        movementStepE = movementStepE * -1
        new_y = y + movementStepEY
        new_x = 380
    elif new_x < -380:
        movementStepE = movementStepE * -1
        new_y = y + movementStepEY
        new_x = -380
    enemy.setposition(new_x, new_y)

turtle.done()

print("-------PROGRAM TERMINATED INTENTIONALLY-------")

这些是错误:

Traceback (most recent call last):
  File "C:/Users/Noname Antilabelson/PycharmProjects/Space Invaders Game/Code/Space Invaders.py", line 97, in <module>
    enemy.setposition(new_x, new_y)
  File "C:\Users\Noname Antilabelson\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 1776, in goto
    self._goto(Vec2D(x, y))
  File "C:\Users\Noname Antilabelson\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 3158, in _goto
    screen._pointlist(self.currentLineItem),
  File "C:\Users\Noname Antilabelson\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 755, in _pointlist
    cl = self.cv.coords(item)
  File "<string>", line 1, in coords
  File "C:\Users\Noname Antilabelson\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2469, in coords
    self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"

对不起,如果我做了任何愚蠢的事情,您的帮助将不胜感激! 问候 - 雅各布·萨顿

【问题讨论】:

  • 传递给enemy.setposition的new_x和new_y的值是多少?你在最后调用之前调用了这个函数两次,那么它有效吗?如果它在之前的两个调用中都有效,我们可以假设该函数是好的。所以看参数。可能为空/错误/超出画布值?
  • 感谢您的仓促回复,是的,数值正常,功能正常运行。
  • 我已经解决了这个错误,它与我关闭窗口时 while true 循环没有退出有关,如果我打破循环,错误将不再出现。

标签: python tkinter turtle-graphics


【解决方案1】:

正如您在评论中指出的那样,while True: 是您的问题,因为它在乌龟这样的事件驱动环境中没有位置。关闭窗口是一个与主循环异步触发的事件。为了使其同步,我们可以使用ontimer() 事件。我在下面对您的代码进行了更改,并加入了其他海龟习语和代码清理:

from turtle import Screen, Turtle

print("------------Space Invaders - Python------------")
print("-------------GAME NOT YET COMPLETED------------")
print("This console is simply a status readout.")

def move_left():
    new_x = player.xcor() - movementStepP

    if new_x < -380:
        new_x = -380

    player.setx(new_x)

def move_right():
    new_x = player.xcor() + movementStepP

    if new_x > 380:
        new_x = 380

    player.setx(new_x)

def move_enemy():
    global new_y, movementStepE

    new_x = enemy.xcor() + movementStepE

    if new_x > 380:
        movementStepE *= -1
        new_y += movementStepEY
        new_x = 380
    elif new_x < -380:
        movementStepE *= -1
        new_y += movementStepEY
        new_x = -380

    enemy.setposition(new_x, new_y)

    screen.ontimer(move_enemy, 50)

movementStepE = 2
movementStepEY = -15
movementStepP = 5

screen = Screen()
screen.bgcolor("black")
screen.title("Space Invaders")

borderPen = Turtle(visible=False)
borderPen.speed("fastest")
borderPen.color("white")
borderPen.pensize(3)

borderPen.penup()
borderPen.setposition(-400, -400)
borderPen.pendown()

for _ in range(4):
    borderPen.forward(800)
    borderPen.left(90)

player = Turtle("triangle")
player.speed("fastest")
player.setheading(90)
player.color("blue")
player.penup()
player.setposition(0, -350)

enemy = Turtle("circle")
enemy.speed("fastest")
enemy.color("red")
enemy.penup()
enemy.setposition(-300, 250)

screen.onkeypress(move_left, "Left")
screen.onkeypress(move_right, "Right")
screen.listen()

new_y = 250

move_enemy()

screen.mainloop()

print("-------PROGRAM TERMINATED INTENTIONALLY-------")

进行此更改后,控制台显示:

> python3 test.py
------------Space Invaders - Python------------
-------------GAME NOT YET COMPLETED------------
This console is simply a status readout.
-------PROGRAM TERMINATED INTENTIONALLY-------
>

即使你在敌人移动时关上窗户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2015-12-20
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多