简体   繁体   中英

Python - Tkinter - canvas.move not working

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM