我有一堆具有标记的对象,而canvas.find方法按预期返回所有对象的ID(请参阅底部附近的打印调用),canvas.move命令不会移动对象。
我构建了一个按照我的预期执行的测试脚本,所以我知道该方法在理论上是合理的。
我做错了什么?
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()
答案 0 :(得分:3)
根据此Tkinter Canvas Widget documentation,传递给move命令的参数与find_withtag标记相同。因此,您应该只需将代码更改为:
w.move('key', 500, -250)
答案 1 :(得分:1)
问题是w.find_withtag('key')返回一个元组对象,但w.move需要一个单独的整数id(或标记字符串)
你可以这样做:
for id in w.find_withtag('key'):
w.move(id, 500, -250)
在你的情况下,使用简单的'key'会更好,如果你有一个任意的id列表,你需要循环它们而不是传入它。