How to do it...

In order to create queues in Python, we have to import the Queue class from the queue module. Add the following statement towards the top of our GUI module:

    from threading import Thread 
from queue import Queue

That gets us started.

Next, we create a queue instance:

    def use_queues(self): 
gui_queue = Queue() # create queue instance
print(gui_queue) # print instance

We call the method within our button click event:

    # Button callback
def click_me(self):
self.action.configure(text='Hello ' + self.name.get())
self.create_thread()
self.use_queues()
In the preceding code, we create a local Queue instance that is only accessible within this method. If we wish to access this queue from other places, we have to turn it into a member of our class by using the self keyword, which binds the local variable to the entire class, making it available from any other method within our class. In Python, we often create class instance variables in the __init__(self) method, but Python is very pragmatic and enables us to create those member variables anywhere in the code.

Now we have an instance of a queue. We can prove that this works by printing it out:

In order to put the data into the queue, we use the put command. In order to get the data out of the queue, we use the get command:

    # Create Queue instance   
def use_queues(self):
gui_queue = Queue()
print(gui_queue)
gui_queue.put('Message from a queue')
print(gui_queue.get())

Running the modified code results in the message first being placed in the Queue, then being taken out of the Queue, and then being printed to the console:

We can place many messages into the Queue:

    # Create Queue instance 
def use_queues(self):
gui_queue = Queue()
print(gui_queue)
for idx in range(10):
gui_queue.put('Message from a queue: ' + str(idx))
print(gui_queue.get())

We have placed ten messages into Queue, but we are only getting the first one out. The other messages are still inside Queue, waiting to be taken out in a FIFO fashion:

In order to get all the messages that have been placed into Queue out, we can create an endless loop:

GUI_queues_put_get_loop_endless.py

    # Create Queue instance 
def use_queues(self):
gui_queue = Queue()
print(gui_queue)
for idx in range(10):
gui_queue.put('Message from a queue: ' + str(idx))
while True:
print(gui_queue.get())

Running the preceding code results in the following screenshot:

While this code works, unfortunately, it freezes our GUI. In order to fix this, we have to call the method in its own thread, as we did in the previous recipes.

Let's run our Queue method in a thread:

    # Running methods in Threads 
def create_thread(self):
self.run_thread = Thread(target=self.method_in_a_thread, args=
[8])
self.run_thread.setDaemon(True)
self.run_thread.start()

# start queue in its own thread
write_thread = Thread(target=self.use_queues, daemon=True)
write_thread.start()
    # Button callback
def click_me(self):
self.action.configure(text='Hello ' + self.name.get())
self.create_thread()
# now started as a thread in create_thread()
# self.use_queues()

When we now click the action button, the GUI no longer freezes and the code works:

GUI_queues_put_get_loop_endless_threaded.py

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.63.95