本文介绍了使用Python Turtle进行多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以同时使用两只海龟在一个窗口中同时绘制两个圆?我尝试了这段代码,但是两只乌龟在分开的窗口中绘制
Is there a way to use two turtles at the same time to draw two circles at the same time in one window? I tried this code but two turtles draw in separated windows
from multiprocessing import Process
import turtle
t1=turtle.Turtle()
t2=turtle.Turtle()
def tes1():
t1.speed(0)
i=0
while i < 360:
t1.forward(1)
t1.left(1)
i+=1
def tes2():
t2.speed(0)
i=0
while i < 360:
t2.forward(1)
t2.right(1)
i+=1
if __name__ == '__main__':
p1 = Process(target=tes1)
p1.start()
p2 = Process(target=tes2)
p2.start()
p1.join()
p2.join()
但是有人告诉我尝试多线程,但是这段代码有严重的语义错误!
but somebody told me try multithreading but this code has a bad semantic error!!
import threading
import turtle
t1=turtle.Turtle()
t2=turtle.Turtle()
def tes1():
t1.speed(0)
i=0
while i < 360:
t1.forward(1)
t1.left(1)
i+=1
def tes2():
t2.speed(0)
i=0
while i < 360:
t2.forward(1)
t2.right(1)
i+=1
t = threading.Thread(target=tes1)
t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
t.start()
t3 = threading.Thread(target=tes2)
t3.daemon = True # thread dies when main thread (only non-daemon thread) exits.
t3.start()
最好的建议是多处理或多线程?
And what is the best suggestion multiprocessing or multithreading?
推荐答案
真的有必要让海龟处于不同的线程中吗?那怎么办?
Is it really necessary that the turtles are in different threads? What about this?
import turtle
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.speed(0)
t2.speed(0)
for i in range(360):
t1.forward(1)
t1.left(1)
t2.forward(1)
t2.right(1)
这篇关于使用Python Turtle进行多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!