如何在Tkinter画布上一次移动多个对象?

时间:2019-01-16 22:10:52

标签: python tkinter tkinter-canvas

我有这段代码,它可以移动两个Tkinter画布对象之一。如何使用canvas.move()使其同时移动?

canvas.create_oval(100, 105, 150, 150, fill = 'light blue', \
                    outline = 'green')

canvas.create_oval(200, 205, 150, 150, fill= 'light blue', \
                    outline = 'green')

tkinter.update()

for x in range (1, 100):
    canvas.move(1, 5, 0)
    tkinter.update()
    time.sleep(0.05)

1 个答案:

答案 0 :(得分:1)

这听起来像是tags的工作。您可以类似地标记两个对象,然后要求画布移动以这种方式标记的所有对象:

import tkinter as tk
import time

root = tk.Tk()

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

canvas.create_oval(100, 105, 150, 150, tags="Bob", fill='light blue', outline='green')
canvas.create_oval(200, 205, 150, 150, tags="Bob", fill='light blue', outline='green')

for _ in range(50):
    canvas.move("Bob", 5, 0)
    canvas.update()
    time.sleep(0.05)

root.mainloop()