Home arrow Python arrow Page 3 - Dialogs in wxPython

wxProgressDialog - Python

You are probably already familiar with a few dialogs that can be used in your wxPython applications. Of course, wxPython contains many more dialogs, ranging in complexity from very simple to pretty advanced and covering a variety of topics, from text selection to color selection. In this article, we'll take a look at more of wxPython's dialogs what they are for and how they are placed in an application.

TABLE OF CONTENTS:
  1. Dialogs in wxPython
  2. wxScrolledMessageDialog
  3. wxProgressDialog
  4. ImageDialog
  5. wxDirDialog
  6. wxFileDialog
By: Peyton McCullough
Rating: starstarstarstarstar / 18
August 01, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 ) + '.' )



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: