使用tkinter时为什么会出现这种TypeError?

时间:2009-09-10 16:36:29

标签: python python-3.x tkinter typeerror

所以我从2.6升级到python 3.1.1,然后我运行了一个使用tkinter的旧程序。

我收到以下错误消息,我不记得进入2.6版本。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "C:\myprog.py", line 77, in <lambda>
    self.canvas.bind("<Button-3>", lambda event: myfunc_sub(event))
  File "C:\myprog.py", line 65, in myfunc_sub
    temp_ids = self.canvas.find_overlapping(self.canvas.coords(name)[0], self.canvas.coords(name)[1], self.canvas.coords(name)[2],self.canvas.coords(name)[3])
TypeError: 'map' object is not subscriptable

我很确定这条线

temp_ids = self.canvas.find_overlapping(self.canvas.coords(name)[0], self.canvas.coords(name)[1], self.canvas.coords(name)[2],self.canvas.coords(name)[3])

在旧版本中没问题。我不确定发生了什么变化,因此无法获得每个坐标的方式。

来自tkinter docs (pdf)

  

“。find_enclosed(x1,y1,x2,y2)   返回完全出现在左上角为(x1,y1)且右下角为(x2,y2)的矩形内的所有对象的对象ID列表。

     

.find_overlapping(x1,y1,x2,y2)   与前一个方法类似,但返回与给定矩形共享至少一个点的所有对象的对象ID列表。“

有关如何解决此问题的任何想法?如果您需要更多信息,请告诉我。我有的tkinter版本是8.5,我有空闲3.1.1和python 3.1.1。我知道我提供的pdf链接是8.4,但我无法想象这些功能有变化。

谢谢!

2 个答案:

答案 0 :(得分:3)

从Python 2.X到Python 3.X有几个重大变化 - 其中包括map的功能。

您是否通过2to3运行了脚本?

答案 1 :(得分:2)

self.canvas.coords(name)

返回map object,因为错误状态map对象在python 3中是不可取消的。您需要将coords更改为元组或列表。

您需要将代码更改为:

temp_ids = self.canvas.find_overlapping(*tuple(self.canvas.coords(name)))