non-blocking GUI

Hello,

After reading the non-blocking gui post on the wxpython wiki I do not
know how to solve the following problem.

I have to update a text label to show the numbers of records that are
being inserted on the database (in real time)
.
With the following code:

fd = open(myFile,"r")
      for line in fd:

          #Other operations
          ...........................................
          #A record is inserted
          self.label_counter.SetLabel(str(self.counter))
          self.counter = self.counter + 1

There are no problems....until the main window is minimized, moved or
maximized; is then when the self.label_counter is frozen until the
function ends and shows the total records inserted.

After reading on the wiki the post about non-blocking ui is not clear
to me if I shall use threads to do this. Normally there are between
10.000 and 20.000 inserts. Shall I start a new thread to update the
text label each time a record is inserted??? Should I create a new
type of event, for example, myEvtRecordInserted and do the same as is
done on the post??

Thanks in advance

The problem is perhaps a little simpler than you think. You should
create a single thread that does the loop which reads the data. Doing
this in a thread means the UI won't block while that is happening.

From within that thread, when you need to update the UI you need to
use wx.CallAfter() to cause the main thread to update the UI. Your
code in the worker thread would look something like:

    fd = open(myFile, "r")
    for line in fd:
        <add record ...>
        self.counter += 1
        wx.CallAfter(self.UpdateUI)

In UpdateUI is where you would put the code to update the label or do
whatever else you want.

···

On Mon, Jan 17, 2011 at 11:48 AM, salbefe <salbefe@gmail.com> wrote:

...
After reading on the wiki the post about non-blocking ui is not clear
to me if I shall use threads to do this. Normally there are between
10.000 and 20.000 inserts. Shall I start a new thread to update the
text label each time a record is inserted??? Should I create a new
type of event, for example, myEvtRecordInserted and do the same as is
done on the post??

Thank you Bryan!!

I did as you told me and now is working fine.

···

On 17 ene, 19:12, Bryan Oakley <bryan.oak...@gmail.com> wrote:

On Mon, Jan 17, 2011 at 11:48 AM, salbefe <salb...@gmail.com> wrote:
> ...
> After reading on the wiki the post about non-blocking ui is not clear
> to me if I shall use threads to do this. Normally there are between
> 10.000 and 20.000 inserts. Shall I start a new thread to update the
> text label each time a record is inserted??? Should I create a new
> type of event, for example, myEvtRecordInserted and do the same as is
> done on the post??

The problem is perhaps a little simpler than you think. You should
create a single thread that does the loop which reads the data. Doing
this in a thread means the UI won't block while that is happening.

From within that thread, when you need to update the UI you need to
use wx.CallAfter() to cause the main thread to update the UI. Your
code in the worker thread would look something like:

fd = open\(myFile, &quot;r&quot;\)
for line in fd:
    &lt;add record \.\.\.&gt;
    self\.counter \+= 1
    wx\.CallAfter\(self\.UpdateUI\)

In UpdateUI is where you would put the code to update the label or do
whatever else you want.