[wxPython-users] Re: wxProgressDialog problems
Robin Dunn
robin at alldunn.com
Thu Feb 21 15:31:19 PST 2008
Larry Bates wrote:
> Robin Dunn wrote:
>> Larry Bates wrote:
>>> I am trying to implement a file upload dialog (see code below). It
>>> almost works, except that I can't get the cancel button to respond as
>>> expect. I'm certain it is something really small.
>>
>> Events (like the cancel button event) won't be delivered if program
>> flow is not in the main loop. In real life that probably won't be a
>> problem for most apps, as long as you don't block some event handler
>> in a long running task. In your sample it can be taken care of by
>> using a timer to update the progress dialog. See
>> http://wiki.wxpython.org/LongRunningTasks for ideas on how to approach
>> other situations.
>>
>> class MyApp(wx.App):
>> def OnInit(self):
>> self.callback = Callback("test file upload")
>> self.callback.start(100)
>> self.timer = wx.Timer(self)
>> self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
>> self.timer.Start(200)
>> self.count = 0
>> return True
>>
>> def OnTimer(self, evt):
>> if self.count < 100:
>> self.callback(100, self.count)
>> self.count += 1
>> else:
>> self.callback.complete()
>>
>> app = MyApp(0)
>> app.MainLoop()
>>
>>
>
> Robin,
>
> My problem is that the thing that calls this Callback is WAY down deep in
> other code. I want to register the Callback instance with that and it
> gets called as the file is streamed (once per (65K block) over a WEBDAV
> connection. I don't see how to adapt what you wrote or the examples on
> the wiki like you sent. When the cancel button is pushed it needs to
> return False so I can cancel the streaming upload and work my way back
> through all the layers back to my main program.
>
> Thoughts?
Do the other code in a thread, and make the callback use wx.CallAfter or
post an event so the update of the progress dialog can be done in the
gui thread. If the cancel button was pressed then set a flag that the
download thread will check periodically so it knows to stop (or send the
message in some other thread-safe way).
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list