Home arrow Python arrow Page 2 - Alternative Frames in wxPython

wxDialog - Python

I'm sure you know all about wxPython's primary frame, wxFrame. It is the basis for most of your windows and will most likely continue to be. Of course, though, wxPython offers other things to put controls on. I'm sure you're getting bored of attaching your controls to slight variations of the same exact thing. In this article, we'll explore other frames offered by wxPython, examining what they are for, how they are used and how to change their appearance.

TABLE OF CONTENTS:
  1. Alternative Frames in wxPython
  2. wxDialog
  3. wxWizard
  4. Multiple Document Interface
By: Peyton McCullough
Rating: starstarstarstarstar / 9
August 29, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

We've looked at dialogs several times so far, but what if we need to create our own dialog? By using the wxDialog class, we are able to do so. Let's create a dialog based on wxDialog that displays four radio buttons, asking the user to choose his or her favorite candidate. Additionally, we'll give our dialog a parent window:

from wxPython.wx import *

# Subclass wxDialog to create a dialog

class BallotDialog ( wxDialog ):

   def __init__ ( self, parent ):

      # Call wxDialog's __init__ method

      wxDialog.__init__ ( self, parent, -1, 'Ballot', size = ( 200, 200 ) )

      # Create a panel for our dialog

      self.panel = wxPanel ( self, -1 )

      # Create a label

      self.label = wxStaticText ( self.panel, -1, 'Please choose a candidate:' )

      

      # Create four radio buttons

      self.candidate1 = wxRadioButton ( self.panel, -1, 'R.Q. Peters', style = wxRB_GROUP )

      self.candidate2 = wxRadioButton ( self.panel, -1, 'P.M. Roger' )

      self.candidate3 = wxRadioButton ( self.panel, -1, 'T.A. Waters' )

      self.candidate4 = wxRadioButton ( self.panel, -1, 'M.W. Wise' )

      # Create a button

      self.button = wxButton ( self.panel, 100, 'Submit Ballot' )

      # Link a button click to a method that destroys

      EVT_BUTTON ( self.panel, 100, self.click )

      # Put everything in a wxBoxSizer

      self.sizer = wxBoxSizer ( wxVERTICAL )

      self.sizer.Add ( ( 200, 5 ), 0 )

      self.sizer.Add ( self.label, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      self.sizer.Add ( self.candidate1, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      self.sizer.Add ( self.candidate2, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      self.sizer.Add ( self.candidate3, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      self.sizer.Add ( self.candidate4, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      self.sizer.Add ( self.button, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      # Attach the sizer to the panel

      self.panel.SetSizerAndFit ( self.sizer )

      # Resize the dialog

      self.sizer.Fit ( self )

   # Handles a click to the button

   def click ( self, event ):

      self.EndModal ( wxID_OK )

# Create a window

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Parent Window' )

      # Create a panel for the window

      self.panel = wxPanel ( self, -1 )

      # Add a wxTextCtrl

      self.text = wxTextCtrl ( self.panel, -1 )

      # Create the dialog

      dialog = BallotDialog ( self )

      # Show the window

      self.Show ( True )

      # Show the dialog ( AFTER we show the window )

      dialog.ShowModal()

      # Center the dialog

      dialog.Center()

application = wxPySimpleApp()

Window()

application.MainLoop()

Try to type something in the wxTextCtrl on the window while the dialog is visible. It's not possible. This is because of the ShowModal method. The ShowModal method creates a modal dialog, causing clicks to other windows to be ignored. In most cases, this behavior is ideal. However, if you do not want this behavior, make a modeless dialog by using the Show method.

Another thing I would like to shed light on is the EndModal method of wxDialog. This exits the dialog and returns the value specified. In this case, we return wxID_OK. Alternatively, you can set the value you will return with SetReturnCode, which works with Show.

An interesting feature of wxDialog is the ability to put the dialog in help mode when using Windows. I'm sure you've seen this feature in other applications. By pressing a button at the top of the dialog, your cursor will change, allowing you to click controls and learn more about them. To access this functionality, we have to create the dialog differently. We have to pre-create it. Fortunately, however, this is fairly simple. Let's create an application that takes advantage of this special help functionality:

from wxPython.wx import *

class HelpDialog ( wxDialog ):

   def __init__ ( self, parent ):

      # Pre-create our dialog

      preCreation = wxPreDialog()

      # Set the wxDIALOG_EX_CONTEXTHELP style

      # This allows for the help functionality

      preCreation.SetExtraStyle ( wxDIALOG_EX_CONTEXTHELP )

      # Create it

      preCreation.Create ( parent, -1, 'Help Dialog' )

      # Do post-creation

      # This is required to get our dialog working

      self.PostCreate ( preCreation )

      # Set the help provider

      # This is necessary to display anything

      wxHelpProvider_Set ( wxSimpleHelpProvider() )

      # Create a panel

      self.panel = wxPanel ( self, -1 )

      # Create a wxTextCtrl

      self.text = wxTextCtrl ( self.panel, -1 )

      # Set the help text

      self.text.SetHelpText ( 'Nice, huh?' )

      # Create a wxButton

      self.button = wxButton ( self.panel, -1, 'Just A Button' )

      # Set the help text

      self.button.SetHelpText ( 'Yes, very nice.' )

      # Add our controls to a sizer

      self.sizer = wxBoxSizer ( wxVERTICAL )

      self.sizer.Add ( ( 200, 5 ), 0 )

      self.sizer.Add ( self.text, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      self.sizer.Add ( self.button, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 5, 5 ), 0 )

      # Attach the sizer to the panel

      self.panel.SetSizerAndFit ( self.sizer )

      # Resize the dialog

      self.sizer.Fit ( self )

application = wxPySimpleApp()

dialog = HelpDialog ( None )

dialog.ShowModal()



 
 
>>> 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: