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
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.
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.
17
#-- Imports --------------------------------------------------------------------
20
import HasTraits, File, Button
23
import View, HGroup, Item
25
from traitsui.file_dialog \
26
import open_file, TextInfo
28
#-- FileDialogDemo Class -------------------------------------------------------
30
# Demo specific file dialig id:
31
demo_id = 'traitsui.demo.standard_editors.file_dialog.text_info'
33
class FileDialogDemo ( HasTraits ):
35
# The name of the selected file:
38
# The button used to display the file dialog:
39
open = Button( 'Open...' )
41
#-- Traits View Definitions ------------------------------------------------
45
Item( 'open', show_label = False ),
47
Item( 'file_name', style = 'readonly', springy = True )
52
#-- Traits Event Handlers --------------------------------------------------
54
def _open_changed ( self ):
55
""" Handles the user clicking the 'Open...' button.
57
file_name = open_file( extensions = TextInfo(),
58
filter = 'Python file (*.py)|*.py',
61
self.file_name = file_name
64
demo = FileDialogDemo()
66
# Run the demo (if invoked from the command line):
67
if __name__ == '__main__':
68
demo.configure_traits()