~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to examples/demo/Standard_Editors/File_Dialog/File_Open_with_TextInfo_Extension.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
This demonstrates using the Traits file dialog with a file dialog extension,
 
3
in this case, the <b>TextInfo</b> extension, which displays (if possible) the
 
4
contents of the currently selected file in a read-only text editor so the user
 
5
can quickly verify they are opening the correct file before leaving the file
 
6
dialog.
 
7
 
 
8
For more information about why you would want to use the Traits file dialog
 
9
over the standard OS file dialog, select the <b>File Open</b> demo. For a
 
10
demonstration of writing a custom file dialog extension, select the
 
11
<b>File Open with Custom Extension</b> demo.
 
12
 
 
13
This example also shows setting a file name filter which only allows Python
 
14
source (i.e. *.py) files to be viewed and selected.
 
15
"""
 
16
 
 
17
#-- Imports --------------------------------------------------------------------
 
18
 
 
19
from traits.api \
 
20
    import HasTraits, File, Button
 
21
 
 
22
from traitsui.api \
 
23
    import View, HGroup, Item
 
24
 
 
25
from traitsui.file_dialog  \
 
26
    import open_file, TextInfo
 
27
 
 
28
#-- FileDialogDemo Class -------------------------------------------------------
 
29
 
 
30
# Demo specific file dialig id:
 
31
demo_id = 'traitsui.demo.standard_editors.file_dialog.text_info'
 
32
 
 
33
class FileDialogDemo ( HasTraits ):
 
34
 
 
35
    # The name of the selected file:
 
36
    file_name = File
 
37
 
 
38
    # The button used to display the file dialog:
 
39
    open = Button( 'Open...' )
 
40
 
 
41
    #-- Traits View Definitions ------------------------------------------------
 
42
 
 
43
    view = View(
 
44
        HGroup(
 
45
            Item( 'open', show_label = False ),
 
46
            '_',
 
47
            Item( 'file_name', style = 'readonly', springy = True )
 
48
        ),
 
49
        width = 0.5
 
50
    )
 
51
 
 
52
    #-- Traits Event Handlers --------------------------------------------------
 
53
 
 
54
    def _open_changed ( self ):
 
55
        """ Handles the user clicking the 'Open...' button.
 
56
        """
 
57
        file_name = open_file( extensions = TextInfo(),
 
58
                               filter     = 'Python file (*.py)|*.py',
 
59
                               id         = demo_id )
 
60
        if file_name != '':
 
61
            self.file_name = file_name
 
62
 
 
63
# Create the demo:
 
64
demo = FileDialogDemo()
 
65
 
 
66
# Run the demo (if invoked from the command line):
 
67
if __name__ == '__main__':
 
68
    demo.configure_traits()
 
69