0

so I upgraded to python 3.1.1 from 2.6 and i ran an old program of mine which uses tkinter.

I get the following error message which I don't recall getting in the 2.6 version.

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

I'm pretty sure the line

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])

was ok in the older version. I'm not sure what has changed so that the way i get each coordinate is not possible.

from the tkinter docs (pdf)

".find_enclosed ( x1, y1, x2, y2 ) Returns a list of the object IDs of all objects that occur completely within the rectangle whose top left corner is (x1, y1) and bottom right corner is (x2, y2).

.find_overlapping ( x1, y1, x2, y2 ) Like the previous method, but returns a list of the object IDs of all the objects that share at least one point with the given rectangle."

any ideas on how to fix this? please let me know if you need more info. the tkinter version i have is 8.5, i have idle 3.1.1 and python 3.1.1. i know the pdf link i provided is for 8.4, but i can't imagine there was a change in these functions.

thanks!

2
  • I don't know if you've noticed but my answer now contains the solution to this question. I'd say this question is exhausted. Commented Sep 10, 2009 at 17:32
  • i noticed. i'm trying to get it to work. as you have clearly understood, i am a beginner.
    – B Rivera
    Commented Sep 10, 2009 at 17:36

2 Answers 2

3

There were several breaking changes from Python 2.X to Python 3.X -- among them, map's functionality.

Have you run your script through 2to3 yet?

5
  • sorry to bug you with this, but at the command line i type "import lib2to3" and then "lib2to3.2to3 myfile.py" and i just get "invalid syntax" errors, i checked that i have lib2to3, but can't seem to get the example code running from the link you sent. is there something special that i have to do to get 2to3 running?
    – B Rivera
    Commented Sep 10, 2009 at 17:09
  • @B Rivera: it needs to be run with python 2.x Commented Sep 10, 2009 at 17:12
  • @SilentGhost: bleh. well that's too bad, this machine i'm on only has python 3.x. and the code was just copied over. my other laptop is on the other side of the atlantic. anything else that i can do other than download python 2.x or go through every line of code?
    – B Rivera
    Commented Sep 10, 2009 at 17:17
  • depends on the size and complexity of your code I suppose. 2to3 wouldn't solve this issue for you. Commented Sep 10, 2009 at 17:29
  • @Mark: I think it's bug / inconsistency across python versions, that possibly cannot be solved with 2to3 Commented Sep 10, 2009 at 17:30
2
self.canvas.coords(name)

return a map object, and as the error states map object is unsubscriptable in python 3. you need to change coords to be a tuple or a list.

you need to change your code to be:

temp_ids = self.canvas.find_overlapping(*tuple(self.canvas.coords(name)))
5
  • i thought <code> self.canvas.coords(name) </code> returns a list a la below. "coords(item) => list Return the coordinates for the given item. If item refers to more than one items, this method returns the coordinates for the first item found."
    – B Rivera
    Commented Sep 10, 2009 at 16:42
  • as can be easily seen in /tkinter/__init__.py Commented Sep 10, 2009 at 17:09
  • ok, thanks. let me ask you this, what does this mean: convertcoords = *tuple(self.canvas.coords(name)) SyntaxError: can use starred expression only as assignment target
    – B Rivera
    Commented Sep 10, 2009 at 17:35
  • because you're not assigning anything, just remove the asterisk if you want to convertecoords to be a tuple. Commented Sep 10, 2009 at 17:39
  • thanks for your fast help, patience, and edits, silentghost. i added the line convertcoords = tuple(self.canvas.coords(name)) into the appropriate spots and everything works fine now.
    – B Rivera
    Commented Sep 10, 2009 at 18:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.