Any time you are required to do large amounts of work in your application, it is a good idea to let the user know your application's progress in the process. A wxProgressDialog will do the job nicely. Let's emulate an application that takes a long time: import time for x in xrange ( 25 ): # Spend some time doing nothing time.sleep ( 2 ) When running it, I have no idea how much progress has been made. This is easily fixed. Let's make a wxProgressDialog and update it after each cycle in the loop: from wxPython.wx import * application = wxPySimpleApp() # Create a dialog to show progress dialog = wxProgressDialog ( 'Progress', 'Doing nothing in a large amount of time.', maximum = 25 ) for x in xrange ( 25 ): # Spend some time doing nothing # We'll use wxSleep rather than time.sleep wxSleep ( 2 ) # Update our progress and change the message dialog.Update ( x + 1, 'On step ' + str ( x + 1 ) + '.' ) Our dialog makes things a lot better. I'm sure you're wondering what the maximum argument is for. Basically, it sets the maximum number that can be set in Update. Once that maximum is hit, the dialog's progress bar will be complete. It's very simple. Because we call the sleep method 25 times, I made maximum match that. You can set maximum to whatever you wish. The default is 100. Notice that as soon as our task is complete, the dialog is hidden from us. This is because of the wxPD_AUTO_HIDE style, which is applied by default. It hides the dialog once maximum is reached. We can change this by manually setting the style: from wxPython.wx import * application = wxPySimpleApp() dialog = wxProgressDialog ( 'Progress', 'Doing nothing in a large amount of time.', maximum = 25, style = wxPD_APP_MODAL ) for x in xrange ( 25 ): wxSleep ( 2 ) dialog.Update ( x + 1, 'On step ' + str ( x + 1 ) + '.' ) If we want to improve our progress dialog even more, we can do so by applying the wxPD_ELAPSED_TIME style, which shows the total amount of time that has gone by since the start of the process: from wxPython.wx import * application = wxPySimpleApp() dialog = wxProgressDialog ( 'Progress', 'Doing nothing in a large amount of time.', maximum = 5, style = wxPD_APP_MODAL | wxPD_ELAPSED_TIME) for x in xrange ( 5 ): wxSleep ( 2 ) dialog.Update ( x + 1, 'On step ' + str ( x + 1 ) + '.' ) To accompany the wxPD_ELAPSED_TIME style, we can apply the wxPD_ESTIMATED_TIME style. It shows the total time that wxPython thinks the process will take: from wxPython.wx import * application = wxPySimpleApp() dialog = wxProgressDialog ( 'Progress', 'Doing nothing in a large amount of time.', maximum = 5, style = wxPD_APP_MODAL | wxPD_ELAPSED_TIME | wxPD_ESTIMATED_TIME ) for x in xrange ( 5 ): wxSleep ( 2 ) dialog.Update ( x + 1, 'On step ' + str ( x + 1 ) + '.' ) Alternatively, we can pass wxPD_REMAINING_TIME. It displays the time wxPython believes is left until the process is finished: from wxPython.wx import * application = wxPySimpleApp() dialog = wxProgressDialog ( 'Progress', 'Doing nothing in a large amount of time.', maximum = 5, style = wxPD_APP_MODAL | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME ) for x in xrange ( 5 ): wxSleep ( 2 ) dialog.Update ( x + 1, 'On step ' + str ( x + 1 ) + '.' )
blog comments powered by Disqus |
|
|
|
|
|
|
|