简体   繁体   中英

Python | Tkinter : Move multiple and different objects to gether

I have searched a lot but, I couldnt find the exact answer whats I need ( or I couldnt understand properly ). In my code there are 1 rectangle, 8 lines. They are moving synchronously. But I want to add a bunch of circles in my canvas. To do that, Im creating a lot of row ''create_oval'' objects. This is meaningless because in next step may be I need thousand of balls in the canvas. I have tried a lot of loops, etc. to find a way to create circles with shortened method but I couldn't achieve what I need. This my codes.

from tkinter import *
from random import randint

tk = Tk()




W, H =600, 500

canvas = Canvas(tk, width=W, height=H)
canvas.pack()

rectangle= canvas.create_rectangle(50, 250, 550, 50, fill="blue")

line1 = canvas.create_line(370, 70, 550,70,fill="black")
line2 = canvas.create_line(330, 90, 550,90,fill="black")
line3 = canvas.create_line(290, 110, 550,110,fill="black")
line4 = canvas.create_line(250, 130, 550,130,fill="black")
line5 = canvas.create_line(210, 150, 550,150,fill="black")
line6 = canvas.create_line(170, 170, 550,170,fill="black")
line7 = canvas.create_line(130, 190, 550,190,fill="black")
line8 = canvas.create_line(90, 210, 550,210,fill="black")

line9 = canvas.create_line(50, 230, 550,230,fill="black")


ball1 = canvas.create_oval(550,50,545,55, fill="red")
ball2 = canvas.create_oval(550,50,545,55, fill="red")
ball3 = canvas.create_oval(550,50,545,55, fill="red")
ball4 = canvas.create_oval(550,50,545,55, fill="red")
ball5 = canvas.create_oval(550,50,545,55, fill="red")
ball6 = canvas.create_oval(550,50,545,55, fill="red")
ball7 = canvas.create_oval(550,50,545,55, fill="red")
ball8 = canvas.create_oval(550,50,545,55, fill="red")

Xspeed = 10
Yspeed = 0




def moveTable():

    global Xspeed,Yspeed    

    canvas.move(table, Xspeed, Yspeed)

    canvas.move(line1, Xspeed, Yspeed)
    canvas.move(line2, Xspeed, Yspeed)
    canvas.move(line3, Xspeed, Yspeed)
    canvas.move(line4, Xspeed, Yspeed)
    canvas.move(line5, Xspeed, Yspeed)
    canvas.move(line6, Xspeed, Yspeed)
    canvas.move(line7, Xspeed, Yspeed)
    canvas.move(line8, Xspeed, Yspeed)
    canvas.move(line9, Xspeed, Yspeed)


    canvas.move(ball1, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball2, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball3, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball4, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball5, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball6, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball7, Xspeed-randint(5,10),Yspeed+randint(5,10))
    canvas.move(ball8, Xspeed-randint(5,10),Yspeed+randint(5,10))

    left,top,right,bot = canvas.coords(table)
    if(left == 60 or left == 40):
        Xspeed = -Xspeed
    
    canvas.after(100, moveTable)


moveTable()

tk.mainloop()

It's probably very simple but I couldn't see it.

If you assign a tag to one or more widgets, you can affect them all at the same time.

Here's an example that moves all of the red items vertically, and all of the green items horizontally, all using only two move commands:

import tkinter as tk
import random

root = tk.Tk()

canvas = tk.Canvas(root, background="black", width=400, height=400)
canvas.pack(fill="both", expand=True)

for i in range(20):
    x = random.randint(10, 110)
    y = random.randint(10, 110)
    width = random.randint(10, 30)
    height = random.randint(10, 30)
    canvas.create_rectangle(x, y, x+width, y+height, fill="red", tags=("red",))
    canvas.create_rectangle(x, y, x+width, y+height, fill="green", tags=("green",))

def animate():
    canvas.move("red", 0, 1)
    canvas.move("green", 1, 0)
    canvas.after(20, animate)

animate()
root.mainloop()

You could also just save the items to a list and iterate over the list

items = []
for i in range(100):
    item = canvas.create_oval(...)
    items.append(item)
...
for item in items:
    canvas.move(item, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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