Python
  Home arrow Python arrow Page 2 - A Look at 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 
Moblin 
JMSL Numerical Library 
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

A Look at wxPython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2005-06-22

    Table of Contents:
  • A Look at wxPython
  • The Basics
  • Creating Windows and Status Bars
  • Creating Menus
  • Events
  • Some Widgets

  • 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


    A Look at wxPython - The Basics


    (Page 2 of 6 )

    Let's take a look at the basics of wxPython by creating a simple text dialog. It will ask the user to input a string of text, and it will then print the string, unless the user exited the dialog by clicking the “X” or the “Cancel” button. In that case, we'll display another message to the user. Here's the code for our application, complete with comments to explain what we're doing:

    # Here we import wxPython:

    from wxPython.wx import *

    # Before we create anything, we must create an "App" object:

    application = wxPySimpleApp()

    # Now we create the dialog:

    dialog = wxTextEntryDialog ( None, 'Enter some text:', 'Title Here', 'Default text here.' )

    # If the user presses, "OK", print the value. Otherwise, print another message.

    if dialog.ShowModal() == wxID_OK:

       print dialog.GetValue()

    else:

       print 'You did not push the "OK" button.'

    # Destroy the dialog

    dialog.Destroy()

    As you can see, the whole process isn't very complicated. The only thing to add to the above example is that the first argument passed to wxTextEntryDialog is the parent of the dialog. Since we're not working with any windows (yet), we pass None.

    Note that we mix our primitive GUI with a console. This is not necessary. Instead of printing the resulting message, we can display it in another dialog:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxTextEntryDialog ( None, 'Enter some text:', 'Title Here', 'Default text here.' )

    if dialog.ShowModal() == wxID_OK:

      

       # Create a "wxMessageDialog" to display the input:

       message = wxMessageDialog ( None, dialog.GetValue(), 'Title Here', wxOK )

       message.ShowModal()

       message.Destroy()

    else:

       # Create another "wxMessageDialog" to display a simple message:

       message = wxMessageDialog ( None, 'You did not push the "OK" button.', 'Title Here', wxOK )

       message.ShowModal()

       message.Destroy()

    dialog.Destroy()

    To get rid of the console completely, save the file with the extension “.pyw” rather than the default extension.

    Notice how we pass wxOK as wxMessageDialog's final argument. This creates our dialog with only an “OK” button. If we need a “Cancel” button, we can take out wxOK and pass nothing:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here' )

    dialog.ShowModal()

    dialog.Destroy()

    We can also pass wxCANCEL in addition to wxOK:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxOK | wxCANCEL )

    dialog.ShowModal()

    dialog.Destroy()

    As you saw earlier, it is very simple to check whether the user clicked “OK” or not:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here' )

    if dialog.ShowModal() == wxOK:

       # User pressed "OK"

       pass

    dialog.Destroy()

    Of course, we can do more to our simple dialog. Instead of “OK” and “Cancel”, we can display “Yes” and “No”:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxYES_NO )

    dialog.ShowModal()

    dialog.Destroy()

    We can also create an icon. Let's make an information icon ( “i” ):

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_INFORMATION )

    dialog.ShowModal()

    dialog.Destroy()

    A question mark icon is also possible:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_QUESTION )

    dialog.ShowModal()

    dialog.Destroy()

    So is an exclamation mark:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_EXCLAMATION )

    dialog.ShowModal()

    dialog.Destroy()

    If we want to notify the user of an error, it is possible to put an error icon in:

    from wxPython.wx import *

    application = wxPySimpleApp()

    dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_ERROR )

    dialog.ShowModal()

    dialog.Destroy()

    More Python Articles
    More By Peyton McCullough


       · Thanks for the good intro, Readers should be sure to check out the 'demo'...
       · Nice basic intro, but there is one major issue. For the last few releases, the...
       · Peyton,Great Introduction for Newbies. You have a knack for being...
     

       

    PYTHON ARTICLES

    - 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
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...





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