views:

369

answers:

2

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  A: 

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?

Mark Rushakoff
no i haven't. looking into it now. let's hope that fixes it.
B Rivera
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
@B Rivera: it needs to be run with python 2.x
SilentGhost
@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
depends on the size and complexity of your code I suppose. 2to3 wouldn't solve this issue for you.
SilentGhost
@Mark: I think it's bug / inconsistency across python versions, that possibly cannot be solved with 2to3
SilentGhost
@Mark: thanks for making me aware of 2to3.
B Rivera
+1  A: 
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)))
SilentGhost
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
well, it clearly doesn't.
SilentGhost
as can be easily seen in `/tkinter/__init__.py`
SilentGhost
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
because you're not assigning anything, just remove the asterisk if you want to `convertecoords` to be a tuple.
SilentGhost
thanks. i need fifteen characters.
B Rivera
and I need 15 points =)
SilentGhost
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