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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PYTHON

Alternative Frames in wxPython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Hey, all. This article just presents some new canvases to work with. Before you read...
     

       

    PYTHON ARTICLES

    - 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
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT