即使使用exitonclick(),也可以使用Python Turtle.Terminator [英] Python Turtle.Terminator even after using exitonclick()

查看:92
本文介绍了即使使用exitonclick(),也可以使用Python Turtle.Terminator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过为turtle创建函数,以使其极其容易绘制形状,并且代码如下所示

I have tried to make functions for turtle to make it extremely easy to draw shapes and the code looks like this

import turtle as t

def square():
     tw = t.Screen()
     for i in range(4):
          t.forward(100)
          t.right(90)
     tw.exitonclick()
def triangle():
     tw = t.Screen()
     for i in range(3):
          t.forward(100)
          t.right(120)
     tw.exitonclick()
def star():
     tw = t.Screen()
     for i in range(5):
          t.forward(150)
          t.right(144)
     tw.exitonclick()

当我在shell中运行此代码时,便会遇到问题...

when i run this code in shell a problem is fonud...

>>> square()
>>> triangle()
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    triangle()
  File "C:\Users\Manop\Desktop\XENON\turtleg.py", line 11, in triangle
    t.forward(100)
  File "<string>", line 5, in forward
turtle.Terminator
>>> star()
>>> square()
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    square()
  File "C:\Users\Manop\Desktop\XENON\turtleg.py", line 5, in square
    t.forward(100)
  File "<string>", line 5, in forward
turtle.Terminator
>>> 

无法解决问题所在,因为我什至使用exitonclick()

can't get over what the problem is, because i even used exitonclick()

推荐答案

在从事学校项目时,我遇到了相同的错误.经过对乌龟库的研究后,我发现了一个名为 TurtleScreen._RUNNING 的变量,如果将此变量设置为 True ,则会打开一个乌龟窗口,如果没有,您将得到> turtle.Terminator 错误.每次关闭海龟屏幕时, TurtleScreen._RUNNING 都会自动设置为 True ,如果要避免这种情况,您只需编写以下代码行 TurtleScreen._RUNNING = True (当然,您需要先导入turtle).

I had the same error while I was working on a school project. After some research on the turtle library I have found a variable called TurtleScreen._RUNNING, if this variable is set to True a turtle window opens, if not you get the turtle.Terminator error. Every time you close a turtle screen, TurtleScreen._RUNNING is automatically set to True, if you want to avoid that, you can simply write this line of code TurtleScreen._RUNNING = True (of course you need to import turtle before).

这篇关于即使使用exitonclick(),也可以使用Python Turtle.Terminator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆