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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" A file explorer example.
Note: This demo only works on the wx backend.
"""
import os
from pyface.api import GUI, PythonShell, SplashScreen
from pyface.api import SplitApplicationWindow, SplitPanel
from pyface.action.api import Action, MenuBarManager, MenuManager
from pyface.action.api import Separator, StatusBarManager, ToolBarManager
from traits.api import Float, Str
from file_filters import AllowOnlyFolders
from file_sorters import FileSorter
from file_table_viewer import FileTableViewer
from file_tree_viewer import FileTreeViewer
class ExampleAction(Action):
""" An example action. """
accelerator = Str("Ctrl-K")
def perform(self):
""" Performs the action. """
print("Performing", self.name)
return
class MainWindow(SplitApplicationWindow):
""" The main application window. """
# 'SplitApplicationWindow' interface -----------------------------------
# The ratio of the size of the left/top pane to the right/bottom pane.
ratio = Float(0.3)
# The direction in which the panel is split.
direction = Str("vertical")
# ------------------------------------------------------------------------
# 'object' interface.
# ------------------------------------------------------------------------
def __init__(self, **traits):
""" Creates a new window. """
# Base class constructor.
super().__init__(**traits)
# Create the window's menu, tool and status bars.
self._create_action_bars()
return
# ------------------------------------------------------------------------
# Protected 'SplitApplicationWindow' interface.
# ------------------------------------------------------------------------
def _create_lhs(self, parent):
""" Creates the left hand side or top depending on the style. """
return self._create_file_tree(parent, os.path.abspath(os.curdir))
def _create_rhs(self, parent):
""" Creates the panel containing the selected preference page. """
self._rhs = SplitPanel(
parent=parent,
lhs=self._create_file_table,
rhs=self._create_python_shell,
direction="horizontal",
)
return self._rhs.control
# ------------------------------------------------------------------------
# Private interface.
# ------------------------------------------------------------------------
def _create_action_bars(self):
""" Creates the window's menu, tool and status bars. """
# Common actions.
highest = Action(name="Highest", style="radio")
higher = Action(name="Higher", style="radio", checked=True)
lower = Action(name="Lower", style="radio")
lowest = Action(name="Lowest", style="radio")
self._actions = [highest, higher, lower, lowest]
# Menu bar.
self.menu_bar_manager = MenuBarManager(
MenuManager(
ExampleAction(name="Foogle"),
Separator(),
highest,
higher,
lower,
lowest,
Separator(),
Action(name="E&xit", on_perform=self.close),
name="&File",
)
)
# Tool bar.
self.tool_bar_manager = ToolBarManager(
ExampleAction(name="Foo"),
Separator(),
ExampleAction(name="Bar"),
Separator(),
ExampleAction(name="Baz"),
Separator(),
highest,
higher,
lower,
lowest,
)
# Status bar.
self.status_bar_manager = StatusBarManager()
return
def _create_file_tree(self, parent, dirname):
""" Creates the file tree. """
self._tree_viewer = tree_viewer = FileTreeViewer(
parent,
input=os.path.abspath(os.curdir),
filters=[AllowOnlyFolders()],
)
tree_viewer.observe(self._on_selection_changed, "selection")
return tree_viewer.control
def _create_file_table(self, parent):
""" Creates the file table. """
self._table_viewer = table_viewer = FileTableViewer(
parent, sorter=FileSorter(), odd_row_background="white"
)
return table_viewer.control
def _create_python_shell(self, parent):
""" Creates the Python shell. """
self._python_shell = python_shell = PythonShell(parent)
python_shell.bind("widget", self._tree_viewer)
python_shell.bind("w", self._tree_viewer)
python_shell.bind("window", self)
python_shell.bind("actions", self._actions)
return python_shell.control
# Trait event handlers -------------------------------------------------
def _on_selection_changed(self, event):
""" Called when the selection in the tree is changed. """
selection = event.new
if len(selection) > 0:
self._table_viewer.input = selection[0]
return
# Application entry point.
if __name__ == "__main__":
# Create the GUI and put up a splash screen (this does NOT start the GUI
# event loop).
gui = GUI(splash_screen=SplashScreen())
# Create and open the main window.
window = MainWindow()
window.open()
# Start the GUI event loop.
gui.start_event_loop()
|