File: File_Open.py

package info (click to toggle)
python-traitsui 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,292 kB
  • sloc: python: 39,867; makefile: 120; sh: 5
file content (103 lines) | stat: -rw-r--r-- 4,305 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
This demonstrates one of the simplest cases of using the Traits file dialog to
select a file for opening (i.e. reading or editing).

The first question of course is why use the Traits file dialog at all, when the
standard OS file dialog is also available?

And the answer is that you can use either, but the advantages of using the
Traits file dialog are:

 - It supports history. That is, each time the user selects a file for opening,
   the file is added to a persistent history list, similar to many applications
   <i>Open recent...</i> function, but built directly into the file dialog.
   The amount of history remembered can be specified by the developer, with the
   default being the last 10 files opened.

 - It is resizable. Some standard OS file dialogs are not resizable, which can
   be very annoying to the user trying to select a file through a tiny
   peephole view of the file system. In addition, if the user resizes the
   dialog, the new size and position will be persisted, so that the file dialog
   will appear in the same location the next time the user wants to open a
   file.

 - There is a very nice synergy between the file system view and the history
   list. Quite often users shuttle between several <i>favorite</i> locations
   in the file system when opening files. The Traits file dialog automatically
   discovers these favorite locations just by the user opening files. When a
   user opens the file dialog, they can select a previously opened file from
   the history list, which then automatically causes the file system view to
   expand the selected file's containing folder, thus allowing them to select a
   different file in the same location. Since the history list is updated each
   time a user selects a file, It tends to automatically discover a <i>working
   set</i> of favorite directories just through simple use, without the user
   having to explicitly designate them as such.

 - It's customizable. The Traits file dialog accepts extension objects which
   can be used to display additional file information or even modify the
   selection behavior of the dialog. Several extensions are provided with
   Traits (and are demonstrated in some of the other examples), and you are
   free to write your own by implementing a very simple interface.

 - The history and user settings are customizable per application. Just by
   setting a unique id in the file dialog request, you can specify that the
   history and window size and position information are specific to your
   application. If you have file dialog extensions added, the user can
   reorder, resize and reconfigure the overall file dialog layout, including
   your extensions, and have their custom settings restored each time they use
   the file dialog. If you do not specify a unique id, then the history and
   user settings default to the system-wide settings for the file dialog. It's
   your choice.

 - It's easy to use. That's what this particular example is all about. So take
   a look at the source code for this example to see how easy it is...
"""

#-- Imports --------------------------------------------------------------------

from traits.api \
    import HasTraits, File, Button

from traitsui.api \
    import View, HGroup, Item

from traitsui.file_dialog  \
    import open_file

#-- FileDialogDemo Class -------------------------------------------------------

class FileDialogDemo ( HasTraits ):

    # The name of the selected file:
    file_name = File

    # The button used to display the file dialog:
    open = Button( 'Open...' )

    #-- Traits View Definitions ------------------------------------------------

    view = View(
        HGroup(
            Item( 'open', show_label = False ),
            '_',
            Item( 'file_name', style = 'readonly', springy = True )
        ),
        width = 0.5
    )

    #-- Traits Event Handlers --------------------------------------------------

    def _open_changed ( self ):
        """ Handles the user clicking the 'Open...' button.
        """
        file_name = open_file()
        if file_name != '':
            self.file_name = file_name

# Create the demo:
demo = FileDialogDemo()

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    demo.configure_traits()