questions to anyone who uses wxPython

Dermot Doran dppdoran at gmail.com
Thu Jul 20 03:57:07 EDT 2006


Hi

the "wxPython in Action" provides a very good explanation as to how to
handle this sort of problem using a combination of pure Python threads and
the wx.CallAfter function.  Also if you want more help on this you can join
the wxPython mailing list via www.wxpython.org.

Here is a small example of what I'm talking about that uses a Queue object
to get a background thread to await work from the main gui thread.  Note it
is VERY important that all GUI updates are performed by the main thread in a
wxPython program.


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()


Cheers!!

Dermot.



On 19 Jul 2006 22:52:09 -0700, Frank Millman <frank at chagford.com> wrote:
>
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060720/7c3763bd/attachment.html>


More information about the Python-list mailing list