Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Turtle.Terminator,即使在使用exitonclick()之后_Python_Python 3.x_Function_Turtle Graphics - Fatal编程技术网

Python Turtle.Terminator,即使在使用exitonclick()之后

Python Turtle.Terminator,即使在使用exitonclick()之后,python,python-3.x,function,turtle-graphics,Python,Python 3.x,Function,Turtle Graphics,我曾尝试为turtle创建函数,使其非常容易绘制形状,代码如下所示 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):

我曾尝试为turtle创建函数,使其非常容易绘制形状,代码如下所示

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中运行此代码时,问题是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
>>> 
>>square()
>>>三角形()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
三角形()
文件“C:\Users\Manop\Desktop\XENON\turtleg.py”,第11行,三角形
t、 前进(100)
文件“”,第5行,向前
乌龟,终结者
>>>星()
>>>正方形()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
正方形()
文件“C:\Users\Manop\Desktop\XENON\turtleg.py”,第5行,正方形
t、 前进(100)
文件“”,第5行,向前
乌龟,终结者
>>> 

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

您的turtle程序结构不正确。你不必这样做:

tw = t.Screen()
...
tw.exitonclick()
在每个功能中
Screen()
只需调用一次
exitonclick()
只能调用一次。尝试以下重组:

import turtle as t

def square():
    for i in range(4):
        t.forward(100)
        t.right(90)

def triangle():
    for i in range(3):
        t.forward(100)
        t.right(120)

def star():
    for i in range(5):
        t.forward(150)
        t.right(144)

t.penup()
t.goto(150, 150)
t.pendown()
square()

t.penup()
t.goto(-150, 150)
t.pendown()
triangle()

t.penup()
t.goto(150, -150)
t.pendown()
star()

screen = t.Screen()
screen.exitonclick()
如果希望以交互方式执行代码,也可以。只需删除函数定义之后的所有内容,以交互方式将其加载到Python中,然后执行以下操作:

>>> star()

或者你想跑什么就跑什么。您不需要调用
Screen()
exitonclick()
在交互工作时没有意义。

您的程序结构不正确。你不必这样做:

tw = t.Screen()
...
tw.exitonclick()
在每个功能中
Screen()
只需调用一次
exitonclick()
只能调用一次。尝试以下重组:

import turtle as t

def square():
    for i in range(4):
        t.forward(100)
        t.right(90)

def triangle():
    for i in range(3):
        t.forward(100)
        t.right(120)

def star():
    for i in range(5):
        t.forward(150)
        t.right(144)

t.penup()
t.goto(150, 150)
t.pendown()
square()

t.penup()
t.goto(-150, 150)
t.pendown()
triangle()

t.penup()
t.goto(150, -150)
t.pendown()
star()

screen = t.Screen()
screen.exitonclick()
如果希望以交互方式执行代码,也可以。只需删除函数定义之后的所有内容,以交互方式将其加载到Python中,然后执行以下操作:

>>> star()

或者你想跑什么就跑什么。您不需要调用
Screen()
exitonclick()
在交互工作时没有意义。

让方法Screen.exitonclick()成为代码中的最后一条语句,而不缩进它

当您使用Pycharm、Spyder等Python IDE时,可以使用此方法

我不知道您是否听说过screen.mainloop()方法

此方法使您能够在Python IDE中运行代码时看到其输出

如果没有此方法,您的输出将在一瞬间显示

我重写了你的代码,这是我的

from turtle import Turtle

t=Turtle()

def square():
    t.up()
    t.setpos(-50,-50)
    t.down()
    for i in range(4):
        t.forward(100)
        t.right(90)

def triangle():
    t.up()
    t.setpos(50,50)
    t.down()
    for i in range(3):
        t.forward(100)
        t.right(120)

def star():
    t.up()
    t.setpos(-200,100)
    t.down()
    for i in range(5):
        t.forward(150)
        t.right(144)

square()
triangle()
star()
t.screen.exitonclick()
这是输出


您还可以检查这个优秀的

让方法screen.exitonclick()成为代码中的最后一条语句,而不缩进它

当您使用Pycharm、Spyder等Python IDE时,可以使用此方法

我不知道您是否听说过screen.mainloop()方法

此方法使您能够在Python IDE中运行代码时看到其输出

如果没有此方法,您的输出将在一瞬间显示

我重写了你的代码,这是我的

from turtle import Turtle

t=Turtle()

def square():
    t.up()
    t.setpos(-50,-50)
    t.down()
    for i in range(4):
        t.forward(100)
        t.right(90)

def triangle():
    t.up()
    t.setpos(50,50)
    t.down()
    for i in range(3):
        t.forward(100)
        t.right(120)

def star():
    t.up()
    t.setpos(-200,100)
    t.down()
    for i in range(5):
        t.forward(150)
        t.right(144)

square()
triangle()
star()
t.screen.exitonclick()
这是输出


您也可以检查这个优秀的

当您中断海龟绘图时,它会生气并产生“异常终止”错误。使用“running”标志在任意点停止进程:

from turtle import Turtle

t=Turtle()

def square():
    global running
    t.up()
    t.setpos(-50,-50)
    t.down()
    for i in range(4):
        if not running: break; # Check 'running' here
        t.forward(100)
        t.right(90)

def triangle():
    global running
    t.up()
    t.setpos(50,50)
    t.down()
    for i in range(3):
        if not running: break; # Check 'running' here
        t.forward(100)
        t.right(120)

def star():
    global running
    t.up()
    t.setpos(-200,100)
    t.down()
    for i in range(5):
        if not running: break; # Check 'running' here
        t.forward(150)
        t.right(144)

def stop(x,y): # x,y are dummy but they are requested
  global running
  running = False  # Disable running

t.screen.onclick(stop) # Set a function for 'running'

running = True  # Enable running

square()
triangle()
star()

我测试了上面的代码。终止始终是平滑的。

当您中断海龟绘图时,它会生气并产生“异常终止”错误。使用“running”标志在任意点停止进程:

from turtle import Turtle

t=Turtle()

def square():
    global running
    t.up()
    t.setpos(-50,-50)
    t.down()
    for i in range(4):
        if not running: break; # Check 'running' here
        t.forward(100)
        t.right(90)

def triangle():
    global running
    t.up()
    t.setpos(50,50)
    t.down()
    for i in range(3):
        if not running: break; # Check 'running' here
        t.forward(100)
        t.right(120)

def star():
    global running
    t.up()
    t.setpos(-200,100)
    t.down()
    for i in range(5):
        if not running: break; # Check 'running' here
        t.forward(150)
        t.right(144)

def stop(x,y): # x,y are dummy but they are requested
  global running
  running = False  # Disable running

t.screen.onclick(stop) # Set a function for 'running'

running = True  # Enable running

square()
triangle()
star()

我测试了上面的代码。终止合同一直都很顺利。

我在学校做项目时也犯了同样的错误。 在对turtle库进行了一些研究之后,我发现了一个名为
TurtleScreen的变量。如果该变量正在运行
,则如果该变量设置为
True
,则会打开一个turtle窗口,否则会出现
turtle.Terminator
错误。
每次你关闭TurtleScreen时,
TurtleScreen.\u RUNNING
会自动设置为
True
,如果你想避免这种情况,你只需编写这行代码
TurtleScreen.\u RUNNING=True
(当然你以前需要导入TurtleScreen)。

我在做学校项目时也犯了同样的错误。 在对turtle库进行了一些研究之后,我发现了一个名为
TurtleScreen的变量。如果该变量正在运行
,则如果该变量设置为
True
,则会打开一个turtle窗口,否则会出现
turtle.Terminator
错误。
每次你关闭TurtleScreen时,
TurtleScreen.\u RUNNING
会自动设置为
True
,如果你想避免这种情况,你只需编写这行代码
TurtleScreen.\u RUNNING=True
(当然你之前需要导入TurtleScreen)。

不起作用(我已经复制粘贴并按原样运行代码)。单击海龟屏幕上的任何地方——甚至多次——都会被忽略。@Apostolos,在绘图完成后,单击屏幕上的AnyWare会干净地退出程序(在Python3中)。您似乎正在解决一个不同的问题:能够在绘图过程中的任何时候干净地退出。我在OP的问题中没有看到任何暗示这种欲望的东西。虽然这是一个有趣的问题,如果这是他真正想要的。是的,只有当所有的绘图完成后,而不是任何时候,直到那时。这是不好的,尤其是在绘图需要很长时间的情况下。请尝试我下面的代码,看看你可以随时退出。不工作(我已复制粘贴并按原样运行代码)。单击海龟屏幕上的任何地方——甚至多次——都会被忽略。@Apostolos,在绘图完成后,单击屏幕上的AnyWare会干净地退出程序(在Python3中)。您似乎正在解决一个不同的问题:能够在绘图过程中的任何时候干净地退出。我在OP的问题中没有看到任何暗示这种欲望的东西。虽然这是一个有趣的问题,如果这是他真正想要的。是的,只有当所有的绘图完成后,而不是任何时候,直到那时。这是不好的,尤其是在绘图需要很长时间的情况下。请尝试我下面的代码,看看你可以随时退出。不工作(我已复制粘贴并按原样运行代码)。点击