【发布时间】:2018-11-27 16:28:16
【问题描述】:
我不想在画布中移动已定义的对象。我知道有一个命令可以移动对象(.move),但它只适用于单个项目。那么如何移动由矩形组成的整个定义的对象呢? 像例子中的那个?因为我需要将数百个小物体作为一个整体移动。
x=400
y=400
def player(x,y):
canvas.create_rectangle(x,y,x+50,y+50,fill='black')
canvas.create_rectangle(x,y+50,x+150,y+150,fill='red')
def moveright(coordinates2):
global x
global y
x=x+200
y=y+0
player(x,y)
def moveleft(coordinates3):
global x
global y
x=x-200
y=y+0
player(x,y)
def moveup(coordinates4):
global x
global y
x=x+0
y=y-150
player(x,y)
def moveright(coordinates5):
global x
global y
x=x+0
y=y+150
player(x,y)
canvas.bind_all('<Right>',moveright)
canvas.bind_all('<Left>',moveleft)
canvas.bind_all('<Up>',moveup)
canvas.bind_all('<Down>',movedown)
【问题讨论】:
-
请尽量减少到minimal reproducible example 我们不需要所有绑定的所有代码,只需要与移动相关的代码。我们也不需要几十个画布项目,而只需要一两个就可以解决这个问题。
标签: python tkinter tkinter-canvas