Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python画布中移动定义的对象_Python_Tkinter_Tkinter Canvas - Fatal编程技术网

在python画布中移动定义的对象

在python画布中移动定义的对象,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我想在画布中移动已定义的对象。我知道有一个命令可以移动一个对象(.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')

我想在画布中移动已定义的对象。我知道有一个命令可以移动一个对象(.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)
x=400
y=400
def播放器(x,y):
画布。创建_矩形(x,y,x+50,y+50,fill='black')
画布。创建_矩形(x,y+50,x+150,y+150,fill='red')
def向右移动(坐标2):
全球x
全局y
x=x+200
y=y+0
玩家(x,y)
def向左移动(坐标3):
全球x
全局y
x=x-200
y=y+0
玩家(x,y)
def向上移动(协调4):
全球x
全局y
x=x+0
y=y-150
玩家(x,y)
def向右移动(坐标5):
全球x
全局y
x=x+0
y=y+150
玩家(x,y)
canvas.bind_all(“”,moveright)
canvas.bind_all(“”,左移)
canvas.bind_all(“”,向上移动)
canvas.bind_all(“”,下移)

与您在问题中所说的不同,
move
适用于使用标记的项目组:
canvas.move(,x,y)

以下是一个例子:

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()


def move():
    # move all items with the 'group' tag
    canvas.move('group', 10, 10)


canvas.create_rectangle(10, 10, 30, 30, tags=['group'])
canvas.create_rectangle(20, 40, 50, 70, tags=['group'])
canvas.create_rectangle(60, 50, 80, 60, tags=['group'])

tk.Button(root, text='Move', command=move).pack()
root.mainloop()

请尽量将此降低到一个级别,我们不需要所有绑定的所有代码,只需要与移动相关的代码。我们也不需要几十个画布项目,因为只有一两个就可以解决这个问题。