Python
  Home arrow Python arrow Page 2 - Alternative Frames in wxPython
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Alternative Frames in wxPython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2005-08-29


    Table of Contents:
  • Alternative Frames in wxPython
  • wxDialog
  • wxWizard
  • Multiple Document Interface

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Alternative Frames in wxPython - wxDialog
    ( Page 2 of 4 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek