Queue handler

Now that the queue is getting populated with various actionables, let's create the queue_handler method to process the items in the queue and update the View accordingly.

We define the queue_handler() method in our View class as follows:

def queue_handler(self): try:   while True:     task = self.queue.get_nowait()     if 'game_over' in task:       self.game_over()     elif 'move' in task:       points = [x for point in task['move'] for x in point]       self.canvas.coords(self.snake, *points)     elif 'food' in task:       self.canvas.coords(self.food, *task['food'])     elif 'points_earned' in task:       self.canvas.itemconfigure(self.points_earned, text='Score:                                 {}'.format (task['points_earned']))      self.queue.task_done() except queue.Empty: self.after(100, self.queue_handler) ...

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.