以下代码中的线程库不起作用
Thread library in the following code isn't work
下面的代码有什么问题?
当我 运行 它只显示两个箭头
当然首先是import thread,但是因为报错(no module named 'thread'),所以我改成了import threading
import threading
import turtle
def f(painter):
for i in range(3):
painter.fd(50)
painter.lt(60)
def g(painter):
for i in range(3):
painter.rt(60)
painter.fd(50)
try:
pat=turtle.Turtle()
mat=turtle.Turtle()
mat.seth(180)
thread.start_new_thread(f,(pat,))
thread.start_new_thread(g,(mat,))
turtle.done()
except:
print("hello")
while True:
pass
threaing 和 thread 是单独的模块来处理 python 中的线程。但是 thread 模块在 python3 中被认为已弃用,但为了向后兼容而重命名为 _thread。在您的情况下,我假设您正在尝试将 _thread 模块与 python3.
一起使用
所以你的代码应该如下所示。
import _thread
import turtle
def f(painter):
for i in range(3):
painter.fd(50)
painter.lt(60)
def g(painter):
for i in range(3):
painter.rt(60)
painter.fd(50)
try:
pat=turtle.Turtle()
mat=turtle.Turtle()
mat.seth(180)
_thread.start_new_thread(f,(pat,))
_thread.start_new_thread(g,(mat,))
turtle.done()
except:
print("Hello")
while True:
pass
由于 _thread 模块已弃用,您最好移至 threading 模块。
下面的代码有什么问题? 当我 运行 它只显示两个箭头 当然首先是import thread,但是因为报错(no module named 'thread'),所以我改成了import threading
import threading
import turtle
def f(painter):
for i in range(3):
painter.fd(50)
painter.lt(60)
def g(painter):
for i in range(3):
painter.rt(60)
painter.fd(50)
try:
pat=turtle.Turtle()
mat=turtle.Turtle()
mat.seth(180)
thread.start_new_thread(f,(pat,))
thread.start_new_thread(g,(mat,))
turtle.done()
except:
print("hello")
while True:
pass
threaing 和 thread 是单独的模块来处理 python 中的线程。但是 thread 模块在 python3 中被认为已弃用,但为了向后兼容而重命名为 _thread。在您的情况下,我假设您正在尝试将 _thread 模块与 python3.
一起使用所以你的代码应该如下所示。
import _thread
import turtle
def f(painter):
for i in range(3):
painter.fd(50)
painter.lt(60)
def g(painter):
for i in range(3):
painter.rt(60)
painter.fd(50)
try:
pat=turtle.Turtle()
mat=turtle.Turtle()
mat.seth(180)
_thread.start_new_thread(f,(pat,))
_thread.start_new_thread(g,(mat,))
turtle.done()
except:
print("Hello")
while True:
pass
由于 _thread 模块已弃用,您最好移至 threading 模块。