Replies: 3 comments 2 replies
-
It's not really clear exactly what you're asking here, but here's a quick example:which might help clarify what you're looking for: from pathlib import Path
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Button, DirectoryTree
class DirectoryTreeApp(App):
def compose(self) -> ComposeResult:
yield Button("Navigate to parent directory")
yield DirectoryTree("./")
@on(Button.Pressed)
def update_directory_tree(self) -> None:
tree = self.query_one(DirectoryTree)
assert isinstance(tree.path, Path)
tree.path = tree.path.parent.resolve()
tree.reload()
if __name__ == "__main__":
app = DirectoryTreeApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
Thank you for the solution. |
Beta Was this translation helpful? Give feedback.
-
My solution; if you click on the root node on the top, it will switch to parent if it can: from pathlib import Path
from textual.widgets import DirectoryTree, Tree
from textual.widgets.directory_tree import DirEntry
class MyDirectoryTree(DirectoryTree):
async def on_tree_node_collapsed(self, event: Tree.NodeExpanded[DirEntry]):
event.stop()
dir_entry = event.node.data
if dir_entry is None or not event.node.is_root:
return
assert isinstance(self.path, Path)
parent = self.path.resolve().parent
if self._safe_is_dir(parent):
self.path = parent
await self.reload() Does what is expected. Root directory name will be displayed once you use the feature. If you wanted to select a directory, you should exclude root: def on_directory_tree_directory_selected(self, event: DirectoryTree.DirectorySelected):
if not event.node.is_root:
self.dismiss(event.path) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The
DirectoryTree
widget seems to not have a method for moving to the parent directory (only child items under current directory). What should I do?Beta Was this translation helpful? Give feedback.
All reactions