Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # --- classes ---
- class Ball:
- def __init__(self, canvas, color, x, y):
- self.canvas = canvas
- self.id = canvas.create_oval(x, y, x+25, y+25, fill=color)
- def update(self):
- self.canvas.move(self.id, 1, 1)
- # --- functions ---
- def update():
- ball1.update()
- ball2.update()
- root.after(100, update) # next execution (again) after 100ms
- # --- main ---
- root = tk.Tk()
- canvas = tk.Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
- canvas.pack()
- ball1 = Ball(canvas, 'red', 10, 10)
- ball2 = Ball(canvas, 'blue', 100, 10)
- #update() # first execution immediately
- root.after(100, update) # or first execution after 100ms
- root.mainloop() # start tkinter engine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement