简体   繁体   English

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

[英]How do I move multiple objects at once on a Tkinter canvas?

I have this code, which moves one of the two Tkinter canvas objects. 我有这段代码,它可以移动两个Tkinter画布对象之一。 How do I make it move both at the same time using canvas.move()? 如何使用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)

This sounds like a job for tags . 这听起来像是标签工作。 You can tag your two objects similarly and then ask the canvas to move all objects tagged that way: 您可以类似地标记两个对象,然后要求画布移动以这种方式标记的所有对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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