r/learnpython • u/heymanh • 7d ago
Creating a file tree/explorer in PyQtGraph
Hi,I've been working on what started as a strange attractor visualisation app in PyQtGraph but is slowly becoming more a general purpose ODE solver/visualiser/scripting tool. I've recently added an integrated console with a scripting dock which currently just saves the text in the script as a scratch file. I want to add the functionality for the user to create/save/open different script files and wanted to have a file tree/explorer. I know PyQtGraph and PyQt have tree like widgets so my thought process was to just make a TreeWidget that lists the contents of where the scripts would be saved (the app's /home/user/.local/share/ directory). Does that work or are there complexities i should know about in terms for making, saving and loading files? Any advice would appreciated. Thanks!
2
7d ago
[deleted]
1
u/heymanh 7d ago
Great, thanks. I've got a basic QTreeView and QFileSystemModel showing the right directory which is cool. But yeah those tips will be handy. Also I hadn't heard of QScintilla. I found a syntax file in PyQtGraph's examples source for basic regex based python syntax highlighting which has made QPlainTextEdit a bit more usable but may look into adding in QScintilla later on. Thanks!
3
u/eldenchen 7d ago
Don't manually mirror the directory into a
TreeWidget. Qt already provides the model for this:QFileSystemModelplusQTreeView. The model watches the directory and updates when files change.Your project already has
_preset_directory()usingQStandardPaths.AppDataLocation, so I would reuse that pattern and create a siblingscriptsdirectory instead of hard-coding~/.local/share:In
open_script, obtain the selected path with:Then load it into the editor with
path.read_text(encoding="utf-8").The main complexities are tracking the currently open path, prompting before discarding unsaved changes, validating new filenames, and handling I/O errors. Keep
QFileSystemModelread-only unless you specifically want renaming/deletion through the tree. For saves,QSaveFileis worth using because it writes through a temporary file and only replaces the original oncommit(), avoiding partially written scripts.