From 3f4b83e7a50f475a78051e677a07778adf57d78b Mon Sep 17 00:00:00 2001 From: Taiko2k Date: Sat, 29 Apr 2023 21:06:06 +1200 Subject: [PATCH] Update README.md Add updated file chooser method --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 64d9ef5..50be6d1 100644 --- a/README.md +++ b/README.md @@ -358,6 +358,8 @@ If you were adding a new action icon it would go in `/usr/share/icons/hicolor/sc ## Open file dialog +> :warning: This method is deprecaded, see next section! + Let's make that open button actually show an open file dialog ```python @@ -390,6 +392,44 @@ If you wanted to restrict the file types shown, you could add a filter. For exam self.open_dialog.add_filter(f) ``` +## Adding a file chooser (updated) + +Here we use [***Gtk.FileDialog***](https://docs.gtk.org/gtk4/class.FileDialog.html) to present an open file dialog to the user. + +```python + self.open_dialog = Gtk.FileDialog.new() + self.open_dialog.set_title("Select a File") + + def show_open_dialog(self, button): + self.open_dialog.open(self, None, self.open_dialog_open_callback) + + def open_dialog_open_callback(self, dialog, result): + try: + file = dialog.open_finish(result) + if file is not None: + print(f"File path is {file.get_path()}") + # Handle loading file from here + except GLib.Error as error: + print(f"Error opening file: {error.message}") + +``` + +Adding a filter and setting it as the default: + +```python + f = Gtk.FileFilter() + f.set_name("Image files") + f.add_mime_type("image/jpeg") + f.add_mime_type("image/png") + + filters = Gio.ListStore.new(Gtk.FileFilter) # Create a ListStore with the type Gtk.FileFilter + filters.append(f) # Add the file filter to the ListStore. You could add more. + + self.open_dialog.set_filters(filters) # Set the filters for the open dialog + self.open_dialog.set_default_filter(f) +```` + + ## Adding a button with menu For this there are multiple new concepts we need to introduce: