Alternative Frames in wxPython - wxWizard
(Page 3 of 4 )
We've all seen wizards in all sorts of applications. They exist to make tasks easy. They are found in installers, graphics programs and even wxPython. Using the wizard module, we can create wizards to use in our applications, making them even more user-friendly. Best of all, these wizards are pretty easy to create. Here's a very simple dummy wizard:
from wxPython.wx import *
# The wizard-related classes are here:
from wxPython.wizard import *
application = wxPySimpleApp()
# Create a wizard with the title "A Wizard"
wizard = wxWizard ( None, -1, 'A Wizard' )
# Create three pages for the wizard
# This is very simple
wizardPage1 = wxWizardPageSimple ( wizard )
wizardPage2 = wxWizardPageSimple ( wizard )
wizardPage3 = wxWizardPageSimple ( wizard )
# Add a label to each page
label1 = wxStaticText ( wizardPage1, -1, 'This is the first page.' )
label2 = wxStaticText ( wizardPage2, -1, 'This is the second page.' )
label3 = wxStaticText ( wizardPage3, -1, 'This is the third and final page.' )
# Link the pages together
# 1 to 2 and 2 to 3
wxWizardPageSimple_Chain ( wizardPage1, wizardPage2 )
wxWizardPageSimple_Chain ( wizardPage2, wizardPage3 )
# Run the wizard and display a message if it was completed successfully
# Otherwise, yell at the user
if wizard.RunWizard ( wizardPage1 ):
dialog = wxMessageDialog ( None, 'Wizard finished!', 'Finished', style = wxOK )
dialog.ShowModal()
dialog.Destroy()
else:
dialog = wxMessageDialog ( None, 'Grr!', 'Not Finished', style = wxOK )
dialog.ShowModal()
dialog.Destroy()
# Destroy the wizard
wizard.Destroy()
There are more advanced and flexible methods for creating wizards, too. Let's suppose we are creating an installer with two installation modes: minimal and full. Based on the user's selection, we want to go to different sections of the wizard. This is completely possible, but it is done a little differently from the way we did it above:
from wxPython.wx import *
# Import the wizard-related data
from wxPython.wizard import *
# Create the wizard page that allows the use to select an installation type
# We wills subclass wxPyWizardPage here
class TypePage ( wxPyWizardPage ):
def __init__ ( self, parent ):
# Call __init__
wxPyWizardPage.__init__ ( self, parent )
# Specify None for the next and previous pages
self.next = None
self.previous = None
# Add a wxRadioBox that displays the two installation options
self.box = wxRadioBox ( self, -1, 'Instalation Method', choices = [ 'Minimal', 'Full' ] )
def GetNext ( self ):
# Return a value based on the radio buttons elected
if self.box.GetSelection() == 0:
return minimal
else:
return full
def GetPrev ( self ):
return self.previous
application = wxPySimpleApp()
# Create the wizard
wizard = wxWizard ( None, -1, 'Installer' )
# Create the installation type page
type = TypePage ( wizard )
# Create a page for the minimal mode
minimal = wxWizardPageSimple ( wizard )
minimalText = wxStaticText ( minimal, -1, 'Poof. Your application has been installed minimally.' )
# Create a page for the full mode
full = wxWizardPageSimple ( wizard )
fullText = wxStaticText ( full, -1, 'Bang. Your application has been fully installed.' )
# Create an additional page
register = wxWizardPageSimple ( wizard )
registerText = wxStaticText ( register, -1, 'Please register your product online,nor we will keep nagging you.' )
# Link the pages that have fixed links ( minimal and full )
minimal.SetNext ( register )
full.SetNext ( register )
# Run the wizard
wizard.RunWizard ( type )
# Destroy the wizard
wizard.Destroy()
If you feel inclined to do so, you can easily add an image to your wizard by creating it like this, where “wizard.PNG” is the name of the image:
wizard = wxWizard ( None, -1, 'My Wizard', wxBitmap ( 'wizard.PNG' )
Next: Multiple Document Interface >>
More Python Articles
More By Peyton McCullough