###############################################################################
# Send message to GUI process from a process via Message Queue.
#Note: save a GIF image ["speech-recognition.gif"] with this same in the same folder.
###############################################################################
from Tkinter import *
import Tkinter as tk
import threading
import multiprocessing
import time
message_rx = ""
class message_queue_class(object):
def __init__(self, name):
self.name = name
def do_something(self):
global message_rx
proc_name = multiprocessing.current_process().name
print 'my name: %s :Message received on queue: %s!' % (proc_name, self.name)
message_rx = self.name
class App(threading.Thread):
def __init__(self, tk_root):
self.root = tk_root
threading.Thread.__init__(self)
self.start()
def run(self):
global message_rx
self.root.geometry("1024x810+400+50")
photo=PhotoImage(file='speech-recognition.gif')
# First Create a text box of size 1024x810
text2 = Text(self.root, height=1024, width=810)
loop_Active=True
while loop_Active:
# Get message from the message queue when it arrive. Till them remain blocking here.
obj = queue.get()
obj.do_something()
# Clear the text in the text box before entering new text
text2.delete(1.0,END)
# Embed image into the text box.
text2.image_create(END, image=photo)
text2.tag_configure('big', font=('Verdana', 20, 'bold'))
text2.insert(END,'\n' + message_rx, 'big')
# Now insert image into text box
text2.pack(side=TOP)
##########################################################################
# Create Tx process that would transmit message to message queue
##########################################################################
def tx_process(q):
print "Starting tx_process"
loop_Active=True
# Keep sending message to GUI process, every 5 secs.
while loop_Active:
queue.put(message_queue_class('Hi.. I am Neelkanth'))
time.sleep(5)
queue.put(message_queue_class('I am Cp. Sparrow'))
time.sleep(5)
#######################################################################
# Create a thread that would copy text from thread to GUI.
# GUI will run in main process.
######################################################################
def gui_process(q):
print "Starting GUI process"
root = tk.Tk()
APP = App(root)
root.mainloop()
#####################################################################################
# Main Process Starts Here
#####################################################################################
if __name__ == "__main__":
queue = multiprocessing.Queue()
# creating GUI process
temp = 1
p1 = multiprocessing.Process(target=gui_process, args=(queue,))
# Start GUI process
p1.start()
# creating a new process
p = multiprocessing.Process(target=tx_process, args=(queue,))
# Starting the new process
p.start()
# Wait for the processes to finish
queue.close()
queue.join_thread()
p.join()
p1.join()
# Send message to GUI process from a process via Message Queue.
#Note: save a GIF image ["speech-recognition.gif"] with this same in the same folder.
###############################################################################
from Tkinter import *
import Tkinter as tk
import threading
import multiprocessing
import time
message_rx = ""
class message_queue_class(object):
def __init__(self, name):
self.name = name
def do_something(self):
global message_rx
proc_name = multiprocessing.current_process().name
print 'my name: %s :Message received on queue: %s!' % (proc_name, self.name)
message_rx = self.name
class App(threading.Thread):
def __init__(self, tk_root):
self.root = tk_root
threading.Thread.__init__(self)
self.start()
def run(self):
global message_rx
self.root.geometry("1024x810+400+50")
photo=PhotoImage(file='speech-recognition.gif')
# First Create a text box of size 1024x810
text2 = Text(self.root, height=1024, width=810)
loop_Active=True
while loop_Active:
# Get message from the message queue when it arrive. Till them remain blocking here.
obj = queue.get()
obj.do_something()
# Clear the text in the text box before entering new text
text2.delete(1.0,END)
# Embed image into the text box.
text2.image_create(END, image=photo)
text2.tag_configure('big', font=('Verdana', 20, 'bold'))
text2.insert(END,'\n' + message_rx, 'big')
# Now insert image into text box
text2.pack(side=TOP)
##########################################################################
# Create Tx process that would transmit message to message queue
##########################################################################
def tx_process(q):
print "Starting tx_process"
loop_Active=True
# Keep sending message to GUI process, every 5 secs.
while loop_Active:
queue.put(message_queue_class('Hi.. I am Neelkanth'))
time.sleep(5)
queue.put(message_queue_class('I am Cp. Sparrow'))
time.sleep(5)
#######################################################################
# Create a thread that would copy text from thread to GUI.
# GUI will run in main process.
######################################################################
def gui_process(q):
print "Starting GUI process"
root = tk.Tk()
APP = App(root)
root.mainloop()
#####################################################################################
# Main Process Starts Here
#####################################################################################
if __name__ == "__main__":
queue = multiprocessing.Queue()
# creating GUI process
temp = 1
p1 = multiprocessing.Process(target=gui_process, args=(queue,))
# Start GUI process
p1.start()
# creating a new process
p = multiprocessing.Process(target=tx_process, args=(queue,))
# Starting the new process
p.start()
# Wait for the processes to finish
queue.close()
queue.join_thread()
p.join()
p1.join()