Could someone please show me how to make this work please? It's the Asynchronous thread example from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 bent around a little bit. My problem is creating the gui instance within the ThreadedClient class. I just can't seem to get it right.
<<omitting the imports etc.. >>
ID_ABOUT=101
ID_EXIT=110
ID_REFRESH=111
myfile = "test.html"
class App(wxFrame):
def __init__(self,parent,id,title,queue, endCommand): #
wxFrame.__init__(self,parent,-4, title, size = ( 800,600), style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
self.queue = queue
self.html = wxHtmlWindow(self, 1)
self.html.SetPage( default_page )
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Setting up the menu.
filemenu= wxMenu()
filemenu.Append(ID_REFRESH, "&Refrsh"," Information about this program")
filemenu.Append(ID_ABOUT, "&About"," Information about this program")
filemenu.AppendSeparator()
filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
# Creating the menubar.
menuBar = wxMenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
EVT_MENU(self, 110, self.Exit)
EVT_MENU(self, 111, self.Refresh_Browser)
self.Show(true)
def processIncoming(self):
"""
Handle all the messages currently in the queue (if any).
"""
while self.queue.qsize():
try:
msg = self.queue.get(0)
# Check contents of message and do what it says
# As a test, we simply print it
print msg
except:
pass
def Exit(self, event):
self.Close(true)
def Refresh_Browser(self, event):
self.html.LoadPage(myfile)
class ThreadedClient:
"""
Launch the main part of the GUI and the worker thread. periodicCall and
endApplication could reside in the GUI part, but putting them here
means that you have all the thread controls in a single place.
"""
def __init__(self, master):
"""
Start the GUI and the asynchronous threads. We are in the main
(original) thread of the application, which will later be used by
the GUI. We spawn a new thread for the worker.
"""
self.master = master
# Create the queue
self.queue = Queue.Queue()
# Set up the GUI part
global gui
self.gui = App(master, self.queue, self.endApplication)
# Set up the thread to do asynchronous I/O
# More can be made if necessary
self.running = 1
self.thread1 = threading.Thread(target=self.workerThread1)
self.thread1.start()
# Start the periodic call in the GUI to check if the queue contains
# anything
self.periodicCall()
def periodicCall(self):
"""
Check every 100 ms if there is something new in the queue.
"""
self.gui.processIncoming()
if not self.running:
# This is the brutal stop of the system. You may want to do
# some cleanup before actually shutting it down.
import sys
sys.exit(1)
self.master.after(100, self.periodicCall)
def workerThread1(self):
"""
This is where we handle the asynchronous I/O. For example, it may be
a 'select()'.
One important thing to remember is that the thread has to yield
control.
"""
while self.running:
# To simulate asynchronous I/O, we create a random number at
# random intervals. Replace the following 2 lines with the real
# thing.
time.sleep(5)
success = "Green Light"
if (success == "Green Light"):
self.queue.put(success)
else:
pass
def endApplication(self):
self.running = 0
root = wxPySimpleApp()
wxImage_AddHandler(wxPNGHandler())
wxImage_AddHandler(wxGIFHandler())
wxImage_AddHandler(wxJPEGHandler())
client = ThreadedClient(root)
root.MainLoop()