【问题标题】:EOFError: Ran out of input while trying to store turtle informationEOFError:尝试存储海龟信息时输入不足
【发布时间】:2020-08-20 00:07:33
【问题描述】:

我正在尝试制作一个海龟显示,以记住我移动对象的位置。为此,我正在使用搁置模块。关于海龟位置的信息,以及 turtle.Turtle() 本身注册在每个海龟的字典内的列表中,如下所示:

Dots = {'A': [turtle, xcor, ycor], 'B': [turtle, xcor, ycor]}

然后写

shelfFile = shelve.open('VisualData')
shelfFile['Dots'] = Dots

并运行一次代码。

然后我像Dots = shelfFile['Dots'] 一样重写第二行并再次运行它,这会导致错误显示。我还在代码末尾写了shelfFile['Dots'] = Dots,以便记录数据中的任何更改。

虽然我知道这很可能是一个非常优雅的解决方案,但我每次使用 shelve 模块时都会使用它并且它确实有效。我在这里真的很茫然。

【问题讨论】:

    标签: python shelve python-turtle


    【解决方案1】:

    我猜你想做这样的事情:

    from turtle import Turtle
    import shelve
    
    yertle = Turtle()
    yertle.goto(100, 100)
    
    myrtle = Turtle()
    myrtle.goto(-200, -200)
    
    dots = {
        'A': [yertle, yertle.xcor(), yertle.ycor()],
        'B': [myrtle, myrtle.xcor(), myrtle.ycor()],
    }
    
    with shelve.open('VisualData') as shelfFile:
        shelfFile['Dots'] = dots
    

    但是当我运行上面的时候,我得到了错误:

    TypeError: cannot pickle '_tkinter.tkapp' object
    

    我发现讨论的内容:

    还有类似的答案。如果我想腌制一只乌龟,我可能会结合position()heading() 来查看pen() 函数,它描述了乌龟的大部分本质并且很容易腌制(如果不是JSON'd):

    from turtle import Turtle
    import shelve
    
    yertle = Turtle()
    yertle.color('green')
    yertle.goto(100, 100)
    
    myrtle = Turtle()
    myrtle.color('red')
    myrtle.goto(-200, -200)
    myrtle.right(45)
    
    dots = {
        'A': [yertle.pen(), yertle.position(), yertle.heading()],
        'B': [myrtle.pen(), myrtle.position(), myrtle.heading()],
    }
    
    with shelve.open('VisualData') as shelfFile:
        shelfFile['Dots'] = dots
    

    然后我们可以使用以下方法恢复我们的海龟:

    from turtle import Screen, Turtle
    import shelve
    
    screen = Screen()
    
    with shelve.open('VisualData') as shelfFile:
        dots = shelfFile['Dots']
    
        pen, position, heading = dots['A']
        yertle = Turtle()
        yertle.pen(pen)
        yertle.penup()
        yertle.setposition(position)
        yertle.setheading(heading)
    
        pen, position, heading = dots['B']
        myrtle = Turtle()
        myrtle.pen(pen)
        myrtle.penup()
        myrtle.setposition(position)
        myrtle.setheading(heading)
    
    yertle.pendown()
    yertle.circle(100)
    
    myrtle.pendown()
    for _ in range(4):
        myrtle.forward(100)
        myrtle.right(90)
    
    screen.exitonclick()
    

    我们应该从海龟被腌制之前所在的位置开始绘制这些新图,并以它们原来的颜色等方式绘制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 2013-03-29
      • 2014-09-22
      • 2017-10-01
      相关资源
      最近更新 更多