Home arrow Python arrow Page 6 - Dialogs in wxPython

wxFileDialog - Python

You are probably already familiar with a few dialogs that can be used in your wxPython applications. Of course, wxPython contains many more dialogs, ranging in complexity from very simple to pretty advanced and covering a variety of topics, from text selection to color selection. In this article, we'll take a look at more of wxPython's dialogs what they are for and how they are placed in an application.

TABLE OF CONTENTS:
  1. Dialogs in wxPython
  2. wxScrolledMessageDialog
  3. wxProgressDialog
  4. ImageDialog
  5. wxDirDialog
  6. wxFileDialog
By: Peyton McCullough
Rating: starstarstarstarstar / 18
August 01, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The last dialog we are going to examine in this article is the wxFileDialog. It allows the user to save or open a file. Setting up a basic dialog is pretty simple. Let's set up one that allows the user to select a file to open:

from wxPython.wx import *

application = wxPySimpleApp()

# Create an open file dialog

dialog = wxFileDialog ( None, style = wxOPEN )

# Show the dialog and get user input

if dialog.ShowModal() == wxID_OK:

   print 'Selected:', dialog.GetPath()

# The user did not select anything

else:

   print 'Nothing was selected.'

# Destroy the dialog

dialog.Destroy()

Let's make our dialog more complicated. First, let's create some filters so the user can select a file type from a list. Second, let's display a message on the dialog. Third, let's allow multiple files to be selected:

from wxPython.wx import *

application = wxPySimpleApp()

# Create a list of filters

# This should be fairly simple to follow, so no explanation is necessary

filters = 'All files (*.*)|*.*|Text files (*.txt)|*.txt'

dialog = wxFileDialog ( None, message = 'Open something....', wildcard = filters, style = wxOPEN | wxMULTIPLE )

if dialog.ShowModal() == wxID_OK:

   # We'll have to make room for multiple files here

   selected = dialog.GetPaths()

   for selection in selected:

      print 'Selected:', selection

else:

   print 'Nothing was selected.'

dialog.Destroy()

Now let's create a dialog that allows us to save files. This is done by simply changing the style wxOPEN to wxSAVE:

from wxPython.wx import *

application = wxPySimpleApp()

# Create a save file dialog

dialog = wxFileDialog ( None, style = wxSAVE )

# Show the dialog and get user input

if dialog.ShowModal() == wxID_OK:

   print 'Selected:', dialog.GetPath()

# The user did not select anything

else:

   print 'Nothing was selected.'

# Destroy the dialog

dialog.Destroy()

A lot of applications present the user with a confirmation dialog if he or she selects a file that already exists. This can be accomplished in your own application by using wxOVERWRITE_PROMPT:

from wxPython.wx import *

application = wxPySimpleApp()

# Create a save file dialog

dialog = wxFileDialog ( None, style = wxSAVE | wxOVERWRITE_PROMPT )

# Show the dialog and get user input

if dialog.ShowModal() == wxID_OK:

   print 'Selected:', dialog.GetPath()

# The user did not select anything

else:

   print 'Nothing was selected.'

# Destroy the dialog

dialog.Destroy()

Conclusion

We've looked at a variety of dialogs that wxPython provides. Dialogs allow programmers to implement common features in their applications without doing much work at all. Instead of working with complex controls to do a simple task, a programmer can call a few methods and have it all done for him or her. All dialogs are simple to create and manage, as this article has thrown through examples. They are worth using in many wxPython applications both large and small.



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