2

I have a bunch of objects that have a tag, and while the canvas.find method returns the IDs for the all the object as expected (see the print call near the bottom), the canvas.move command doesn't move the objects.

I build a test script that performs as I expect, so I know the method is in theory sound.

What am I doing wrong?

from Tkinter import * 
master = Tk()
w = Canvas(master, width=1000, height=1000)
w.config(bg='white')
box=25
startX=100
startY=800
rows = 5
columns = 6
coords=[[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]],[[],[],[],[],[]]]
widths=[[2,2,2,2,2,2],[2,2,2,2,2,2],[2,2,2,2,2,2],[2,2,2,2,2,2],[2,2,2,2,2,2]]
tagsList = [["a","a","a","a","a","a"],["a","a","a","a","a","a"],["a","a","a","a","a","a"],["a","a","a","a","a","a"],["a","a","a","a","a","a"]]
for j in range(1, 7):
 for i in range(1, 6):
  coords[j-1][i-1]=[(startX)+(box*(j-1)),(startY)+(box*(i-1)),(startX)+(box*j),(startY)+(box*i)]
colours=[["white","#660000","#863030","#ba4a4a","#de7e7e","#ffaaaa"],["white","#a34b00","#d46200","#ff7a04","#ff9b42","#fec28d"],["white","#dfd248","#fff224","#eefd5d","#f5ff92","#f9ffbf"],["white","#006600","#308630","#4aba4a","#7ede7e","#aaffaa"],["white","white","white","white","white","white"]]
w.create_text(startX+(box*columns)/2, startY-(box/1.2), text="Key:", justify = "center", font=("Helvetica", 20),tag="key")
w.create_text((startX-(box*1.5)),(startY+(box*(rows-1)/2)), text="No. \nDroids", justify = "center", font=("Helvetica", 16),tag="key")
w.create_text((startX+(box*columns)/2,(startY+box*(rows))+(box/1.5)), text="No. Sigs", justify = "center", font=("Helvetica", 16),tag="key")
w.create_text((startX+(box*(columns-4))-box/2,(startY+box*(rows-1))+(box/1.9)), text="5", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #5
w.create_text((startX+(box*(columns-3))-box/2,(startY+box*(rows-1))+(box/1.9)), text="4", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #4
w.create_text((startX+(box*(columns-2))-box/2,(startY+box*(rows-1))+(box/1.9)), text="3", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #3
w.create_text((startX+(box*(columns-1))-box/2,(startY+box*(rows-1))+(box/1.9)), text="2", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #2
w.create_text((startX+(box*(columns))-box/2,(startY+box*(rows-1))+(box/1.9)), text="1", justify = "center",font=("Helvetica", 16),tags=("key", "b")) #1
w.create_text((startX+box/2,(startY+box*(rows-5))+(box/2)), text="1", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #1
w.create_text((startX+box/2,(startY+box*(rows-4))+(box/2)), text="2", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #2
w.create_text((startX+box/2,(startY+box*(rows-3))+(box/2)), text="3", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #3
w.create_text((startX+box/2,(startY+box*(rows-2))+(box/2)), text="4", justify = "center", font=("Helvetica", 16),tags=("key", "b")) #4
for i in range(0, 5):
 for j in range(0, 6):
  w.create_rectangle(*coords[j][i],width=widths[i][j],tags=(tagsList[i][j],"key"),fill=colours[i][j])   
w.tag_raise("b")
w.move(w.find_withtag('key'), 500, -250)
w.pack()
print str(w.find_withtag('key')) 
w.update()
mainloop()

2 Answers 2

3

According to this Tkinter Canvas Widget documentation, the argument passed into the move command is the same as the find_withtag tag. So you should be able to simply change the code to this:

w.move('key', 500, -250)
Sign up to request clarification or add additional context in comments.

5 Comments

I understand that, thats why I used print str(w.find_withtag('key')) to demonstrate that the tags are being collected by w.find_withtag
RE: your edit - Yupe. That works. How frustrating. How did you know that the tag is addressed directly, and not indirectly (the way I was trying)? Thank you.
Which I did... What I didn't get was that an 'Item specifier' is not the ID number, but a tag - had the doc said that, I would have known!... I'm not complaining, I'm trying to learn, and curious what lead you to know that the tag was a specifier. (Given that in a test I did, my method worked for a few items...). As I said, I really appreciate your help - I've been trying to get this running for a few hours :(
@JayGattuso: No problem. I've been many times in your situation, so I well understand;-) Sometimes some things just aren't clear to everyone, and it takes a different perspective to clear it up.
@Jay Gattuso: tkinter documentation can sometimes be a bit incomplete. Tkinter is a wrapper around the tk toolkit which is very well documented, and the translation from tcl/tk to Tkinter is usually trivial. For definitive documentation you might try reading the official tk docs at tcl.tk/man/tcl8.5/TkCmd/contents.htm In the canvas documentation there is a section on what is a valid identifier, including a sentence saying you can use a tag name or an actual id.
1

The issue is that w.find_withtag('key') returns a tuple object, but w.move expects an individual integer id (or a tag string)

You could do:

for id in w.find_withtag('key'):
    w.move(id, 500, -250)

While in your case, using simple 'key' is better, if you had an arbitrary list of ids, you would need to loop though them instead of passing it in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.