import wx
import threading
import Queue
class WorkerThread(threading.Thread):
def __init__(self, callBack):
threading.Thread.__init__(self)
self.callBack = callBack
self.work_queue = Queue.Queue()
self.setDaemon(True)
def run(self):
wx.CallAfter(self.callBack, "Thread starting up...")
work_to_be_done = True
while work_to_be_done:
req = self.work_queue.get(True)
# Go and perform some long lasting task here!
wx.CallAfter(self.callBack, "Sorry I was kind of busy just now!")
def helloThere(self):
self.work_queue.put_nowait("This could be an object")
class MyFrame(
wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Testing wxCallback and Python threads")
self.worker_thread = WorkerThread(self.logMessage)
panel = wx.Panel(self)
test_btn = wx.Button(panel, -1, "Hello!")
self.log = wx.TextCtrl(panel, -1, "", style=wx.TE_MULTILINE|wx.TE_RICH2|wx.TE_READONLY)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.log
, 1, wx.EXPAND|wx.ALL, 5)
sizer.Add(test_btn, 0, wx.ALIGN_CENTER_HORIZONTAL)
panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.onTestBtn, test_btn)
self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
self.worker_thread.start()
def onTestBtn(self, evt):
self.worker_thread.helloThere()
def onCloseWindow(self, evt):
self.Destroy()
def logMessage(self, msg):
self.log.AppendText
(msg)
self.log.AppendText("\n")
if __name__ == '__main__':
app = wx.PySimpleApp()
frm = MyFrame()
frm.Show()
app.MainLoop()
damacy wrote:
> hello. i'm using wxPython as my GUI package and whenever my program
> executes a long process which takes at least 2 or 3 seconds, the user
> interface gets corrupted while executing the progrocess during the
> period.
>
> i have tried the following lines of code...
>
> frame = mainwindow(None, -1, 'my program')
> ...
> ...
> frame.UpdateWindowUI()
>
> and it did not make any difference at all.
>
> could anyone help me?
I don't really understand the question - what do you mean when you say
the user interface gets corrupted?
Nevertheless, the following pointer may help -
http://tinyurl.com/hj84l
It is an article in the wxPyWiki that discusses various ways of
handling longrunning tasks.
HTH
Frank Millman
--
http://mail.python.org/mailman/listinfo/python-list
-- http://mail.python.org/mailman/listinfo/python-list