views:

44

answers:

3

The update method of wx.ProgressDialog has a newmsg argument that is supposed to give a textual update on what is happening in each step of the process, but my code is not doing this properly.

Here is the link to the documentation for wx.ProgressDialog http://www.wxpython.org/docs/api/wx.ProgressDialog-class.html

Also, when I run my code, the progress bar itself stops updating when it looks to be about 50 percent complete.

Can anyone show me how to fix these two problems?

Here is my code:

import wx
import time

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="ProgressDialog sample")
        self.progressMax = 4
        self.count = 0
        self.newstep='step '+str(self.count)
        self.dialog = None
        self.OnTimer(self.count)

    def OnTimer(self, evt):
        if not self.dialog:
            self.dialog = wx.ProgressDialog("Progress in processing your data.",
                                        self.newstep,
                                        self.progressMax,
                                        style=wx.PD_CAN_ABORT
                                        | wx.PD_APP_MODAL
                                        | wx.PD_SMOOTH
                                        | wx.PD_AUTO_HIDE)
        # Do Step One
        print '----------------------------'
        print 'Starting Step One now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Do Step Two
        print '----------------------------'
        print 'Starting Step Two now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Do Step Three
        print '----------------------------'
        print 'Starting Step Three now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Do Step Four
        print '----------------------------'
        print 'Starting Step Four now.'
        self.count += 1
        self.newstep='step '+str(self.count)
        print 'self.count is:  ',self.count
        print 'self.newstep is:  ',self.newstep
        keepGoing = self.dialog.Update(self.count,self.newstep)
        print '----------------------------'
        time.sleep(5)

        # Delete the progress bar when it is full
        self.dialog.Update(self.progressMax)
        time.sleep(3)
        self.dialog.Destroy()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = Frame(None)
    frame.Show()
    app.MainLoop()

Notice that I am printing everything out in order to check progress. The result of the print commands is different than what is shown in the progress dialog. The print commands seem to be doing what the code is telling them to do, but the progress dialog does not seem to be doing what the code is telling it to do, and the progress dialog is not in agreement with the result of the print commands.
This is in version 2.6 of Python.

EDIT One:

-------------------------------------------------------------------------------

I edited the code above to match adw's suggestions. The problem of not updating newmsg seems to have been eliminated, but the progress bar itself still seems to only get 50% full, and that happens when the newmsg output in the dialog box says "step 3". The progress bar then disappears. A non-computer-person using this software might realistically think that the process only completed about 50% of its work, having quit early in step 3. How can I edit the code so that it shows "step 4" in the dialog box, and so that the progress bar actually fills up to 100% for a second or two before the ProgressDialog is killed?

Edit Two:

------------------------------------------------------------------------------

I added the changes that ChrisC suggested to the code above, as you can see. But running this newly altered code still gives the same problem. So the suggestion does not seem to work in the form that I understood when I edited the code above. Can you suggest something specific I can do to the above code? Is there anything that makes it work on your computer?

+1  A: 

Python identifiers are case sensitive, so newstep is different from newStep.

As for the bar itself, that works properly when I run your code. Although I had to change (keepGoing, skip) to keepGoing everywhere, probably a version difference (I have wx.VERSION_STRING == '2.6.3.2').

adw
A: 

Can you call self.dialog.Update(self.progressMax) before self.dialog.Destroy(), maybe with a time.sleep(1) thrown in for a visible pause?

ChrisC
+1  A: 

Your problem is wx.PD_AUTO_HIDE. As it says in the documentation, using that style

Causes the progress dialog to disappear from screen as soon as the maximum value of the progress meter has been reached.

Change your code to

self.dialog = wx.ProgressDialog("Progress in processing your data.",
                                    self.newstep,
                                    self.progressMax,
                                    style=wx.PD_CAN_ABORT
                                    | wx.PD_APP_MODAL
                                    | wx.PD_SMOOTH)

Then the code ChrisC suggested will work.

Velociraptors
Just tried it and works as advertised for me on Ubuntu. Voted up because I'd just figured out a minute ago that the problem was with Update() but hadn't thought to look at the style. :)
ChrisC
I checked the styles first because I had the same problem the first time I used ProgressDialog
Velociraptors