当我运行我制作的脚本时,我得到一个非常奇怪的错误。我正在线程化两个函数,move()和moveWithMouse()。相关代码发布在这里:
def moving():
# Clearing the canvas and hiding the turtle for the next iteration of moving()
turtle.clear()
turtle.hideturtle()
# Drawing all of the circles
for i in range(len(xCoordinate)):
turtle.penup()
turtle.goto(xCoordinate[i], yCoordinate[i])
turtle.pendown()
turtle.fillcolor(color[i][0], color[i][1], color[i][2])
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
xCoordinate[i] += speed1[i]
yCoordinate[i] += speed2[i]
turtle.update()
turtle.ontimer(moving, 10)
下一个功能的代码:
def moveWithMouse():
while True:
user = win32api.GetCursorPos()
mousepos = [user[0]-520,-1*(user[1])+ 415]
turtle.goto(mousepos)
turtle.onclick(turtle.pendown())
然后我像这样编写这两个函数:
if __name__ == '__main__':
Thread(target = moving).start()
Thread(target = moveWithMouse).start()
它会给我这个错误(它很长,但我仍然认为发布所有这些都是必要的):
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python32\lib\threading.py", line 736, in _bootstrap_inner
self.run()
File "C:\Python32\lib\threading.py", line 689, in run
self._target(*self._args, **self._kwargs)
File "C:\Python33\Circles with Collision Detection.py", line 57, in moving
turtle.circle(10)
File "<string>", line 1, in circle
File "C:\Python32\lib\turtle.py", line 1991, in circle
self._go(l)
File "C:\Python32\lib\turtle.py", line 1605, in _go
self._goto(ende)
File "C:\Python32\lib\turtle.py", line 3159, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Python32\lib\turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Python32\lib\tkinter\__init__.py", line 2162, in coords
self.tk.call((self._w, 'coords') + args))]
File "C:\Python32\lib\tkinter\__init__.py", line 2160, in <listcomp>
return [getdouble(x) for x in
ValueError: could not convert string to float: 'coords'
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python32\lib\threading.py", line 736, in _bootstrap_inner
self.run()
File "C:\Python32\lib\threading.py", line 689, in run
self._target(*self._args, **self._kwargs)
File "C:\Python33\Circles with Collision Detection.py", line 128, in moveWithMouse
turtle.goto(mousepos)
File "<string>", line 1, in goto
File "C:\Python32\lib\turtle.py", line 1774, in goto
self._goto(Vec2D(*x))
File "C:\Python32\lib\turtle.py", line 3159, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Python32\lib\turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Python32\lib\tkinter\__init__.py", line 2162, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: ambiguous option "": must be addtag, bbox, bind, canvasx, canvasy, cget, configure, coords, create, dchars, delete, dtag, find, focus, gettags, icursor, index, insert, itemcget, itemconfigure, lower, move, postscript, raise, scale, scan, select, type, xview, or yview
它似乎说这两个函数中的goto语句都有问题。这两个函数在我没有线程化时都有效,但是线程化,它们似乎给出了这个非常奇怪的错误。有什么想法吗?
答案 0 :(得分:0)
turtle
模块基于Tkinter
。 Tkinter
被设计为单线程。也就是说,为了避免错误,所有Tkinter调用必须来自同一个线程。
moving
和moveWithMouse
都在调用turtle
个命令。
所以你不能产生两个线程来运行这些函数。
答案 1 :(得分:0)
Python的turtle模块并不声称是线程安全的(参见bug 1702036)。所以你不能假设移动两只乌龟会按预期工作(这就是你正在做的事情:在两个线程中你都在移动乌龟进行绘图)。