【问题标题】:Turtle graphic screen not responding乌龟图形屏幕没有响应
【发布时间】:2019-01-28 17:30:43
【问题描述】:

所以我试图在屏幕上绘制一些文本,每次我按下乌龟图形屏幕时,它都会变得无响应。当我尝试通过添加主循环来修复它时,它不会继续使用其余代码。我看到了我应该添加的地方

done()

在块的末尾,但 python 说它不存在,我尝试放置 turtle.done() 但没有。

代码如下:

def draw_robot(choice_robot,robots):
    stats = robots[choice_robot]
    style = 'Arial',14,'bold'
    t.setheading(-90)
    t.write('Name: '+choice_robot,font=style,align = 'center')
    t.forward(25)
    t.write('Battery: '+stats[0],font=style,align = 'center')
    t.forward(25)
    t.write('Intelligence: '+stats[1],font=style,align = 'center')
    turtle.mainloop()

我该如何解决这个问题?

【问题讨论】:

    标签: python graphics turtle-graphics


    【解决方案1】:

    turtle.mainloop() 不应出现在子例程中。一般来说,它应该是在一页龟代码上执行的最后一件事。即,无论是字面上的最后一条语句,还是 main() 例程所做的最后一件事。它将控制权交给 tkinter 的事件处理程序,在该处理程序中,与海龟的所有交互都是通过事件(击键、鼠标移动等)

    以下是我期望的一个合适的海龟程序的大致布局:

    from turtle import Turtle, Screen  # force Object-oriented interface
    
    STYLE = ('Arial', 14, 'bold')
    
    def draw_robot(choice_robot, robots):
        stats = robots[choice_robot]
    
        t.setheading(-90)
        t.write('Name: ' + choice_robot, font=STYLE, align='center')
        t.forward(25)
        t.write('Battery: ' + stats[0], font=STYLE, align='center')
        t.forward(25)
        t.write('Intelligence: ' + stats[1], font=STYLE, align='center')
    
    screen = Screen()
    
    t = Turtle()
    
    my_choice_robot = None  # whatever ...
    my_robots = None  # whatever ...
    
    draw_robot(my_choice_robot, my_robots)
    
    screen.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2020-09-03
      • 2019-04-23
      • 1970-01-01
      相关资源
      最近更新 更多