SHARE
TWEET

# threading_ovals.py

here2share May 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # threading_ovals.py
  2.  
  3. import threading
  4. import Queue
  5. import random
  6. import math
  7. import time
  8. import Tkinter
  9.  
  10. random.seed(0)
  11.  
  12. class App:
  13.     def __init__(self, queue, width=400, height=300):
  14.         self.width, self.height = width, height
  15.         self.canvas = Tkinter.Canvas(width=width, height=height, bg='black')
  16.         self.canvas.pack(fill='none', expand=False)
  17.         self._oid = []
  18.         self.canvas.after(10, self.move)
  19.  
  20.         self.queue = queue
  21.         self.canvas.after(50, self.check_queue)
  22.  
  23.     def check_queue(self):
  24.         try:
  25.             x, y, rad, outline = self.queue.get(block=False)
  26.         except Queue.Empty:
  27.             pass
  28.         else:
  29.             self.create_moving_ball(x, y, rad, outline)
  30.         self.canvas.after(50, self.check_queue)
  31.  
  32.     def move(self):
  33.         width, height = self.width, self.height
  34.         for i, (oid, r, angle, speed, (x, y)) in enumerate(self._oid):
  35.             sx, sy = speed
  36.             dx = sx * math.cos(angle)
  37.             dy = sy * math.sin(angle)
  38.             if y + dy + r> height or y + dy - r < 0:
  39.                 sy = -sy
  40.                 self._oid[i][3] = (sx, sy)
  41.             if x + dx + r > width or x + dx - r < 0:
  42.                 sx = -sx
  43.                 self._oid[i][3] = (sx, sy)
  44.             nx, ny = x + dx, y + dy
  45.             self._oid[i][-1] = (nx, ny)
  46.             self.canvas.move(oid, dx, dy)
  47.         self.canvas.update_idletasks()
  48.         self.canvas.after(10, self.move)
  49.  
  50.     def create_moving_ball(self, x=100, y=100, rad=20, outline='white'):
  51.         oid = self.canvas.create_oval(x - rad, y - rad, x + rad, y + rad,
  52.                 outline=outline)
  53.         oid_angle = math.radians(random.randint(1, 360))
  54.         oid_speed = random.randint(2, 5)
  55.         self._oid.append([oid, rad, oid_angle, (oid_speed, oid_speed), (x, y)])
  56.  
  57. def queue_create(queue, running):
  58.     while running:
  59.         x, y = random.randint(100, 150), random.randint(100, 150)
  60.         color = random.choice(['green', 'white', 'yellow', 'blue'])
  61.         queue.put((x, y, random.randint(10, 30), color))
  62.         track.append(0)
  63.         print "balls:", len(track)
  64.         time.sleep(2) # Effectively yield this thread.
  65.  
  66. root = Tkinter.Tk()
  67. running = [True]
  68. track = []
  69.  
  70. queue = Queue.Queue()
  71.  
  72. app = App(queue)
  73. app.create_moving_ball()
  74. app.canvas.bind('<Destroy>', lambda x: (running.pop(), x.widget.destroy()))
  75.  
  76. thread = threading.Thread(target=queue_create, args=(queue, running))
  77. thread.start()
  78.  
  79. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top