Home arrow Python arrow Page 2 - A Look at wxPython

The Basics - Python

This article is the first in a series covering wxPython, a library that makes it easy for Python programmers to build graphical user interfaces. It introduces some simple dialogs, explains how to create a simple frame, how to create a menu, and how to add two basic controls to a panel.

TABLE OF CONTENTS:
  1. A Look at wxPython
  2. The Basics
  3. Creating Windows and Status Bars
  4. Creating Menus
  5. Events
  6. Some Widgets
By: Peyton McCullough
Rating: starstarstarstarstar / 23
June 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: