Advertisement
furas

Python - Tkinter - move balls

Jul 15th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. # --- classes ---
  4.  
  5. class Ball:
  6.    
  7.     def __init__(self, canvas, color, x, y):
  8.         self.canvas = canvas
  9.         self.id = canvas.create_oval(x, y, x+25, y+25, fill=color)
  10.  
  11.     def update(self):
  12.         self.canvas.move(self.id, 1, 1)
  13.  
  14. # --- functions ---
  15.  
  16. def update():
  17.     ball1.update()
  18.     ball2.update()
  19.     root.after(100, update) # next execution (again) after 100ms
  20.  
  21. # --- main ---
  22.  
  23. root = tk.Tk()
  24. canvas = tk.Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
  25. canvas.pack()
  26.  
  27. ball1 = Ball(canvas, 'red', 10, 10)
  28. ball2 = Ball(canvas, 'blue', 100, 10)
  29.  
  30. #update() # first execution immediately
  31. root.after(100, update) # or first execution after 100ms
  32.  
  33. root.mainloop() # start tkinter engine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement