| 1 |
import os as _os |
| 2 |
|
| 3 |
from concurrent.futures import ProcessPoolExecutor as _ProcessPoolExecutor |
| 4 |
|
| 5 |
from pyglet.event import EventDispatcher as _EventDispatcher |
| 6 |
|
| 7 |
"""File dialog classes for opening and saving files. |
| 8 |
|
| 9 |
This module provides example Dialog Windows for opening and |
| 10 |
saving files. These are made using the `tkinter` module, which |
| 11 |
is part of the Python standard library. Dialog Windows run in a |
| 12 |
background process to prevent any interference with your main |
| 13 |
application, and integrate using the standard pyglet Event |
| 14 |
framework. |
| 15 |
|
| 16 |
Note that these dialogs do not actually open or save any data |
| 17 |
to disk. They simply return one or more strings, which contain |
| 18 |
the final file paths that were selected or entered. You can |
| 19 |
then use this information in your main application to handle |
| 20 |
the disk IO. This is done by attaching an event handler to the |
| 21 |
dialog, which will receive the file path(s) as an argument. |
| 22 |
|
| 23 |
Create a `FileOpenDialog` instance, and attach a handler to it:: |
| 24 |
|
| 25 |
# Restrict to only showing ".png" and ".bmp" file types, |
| 26 |
# and allow selecting more than one file |
| 27 |
open_dialog = FileOpenDialog(filetypes=[("PNG", ".png"), ("24-bit Bitmap", ".bmp")], multiple=True) |
| 28 |
|
| 29 |
@open_dialog.event |
| 30 |
def on_dialog_open(filenames): |
| 31 |
print("list of selected filenames:", filenames) |
| 32 |
# Your own code here to handle loading the file name(s). |
| 33 |
|
| 34 |
# Show the Dialog whenever you need. This is non-blocking: |
| 35 |
open_dialog.show() |
| 36 |
|
| 37 |
|
| 38 |
The `FileSaveDialog` works similarly:: |
| 39 |
|
| 40 |
# Add a default file extension ".sav" to the file |
| 41 |
save_as = FileSaveDialog(default_ext='.sav') |
| 42 |
|
| 43 |
@save_as.event |
| 44 |
def on_dialog_save(filename): |
| 45 |
print("FILENAMES ON SAVE!", filename) |
| 46 |
# Your own code here to handle saving the file name(s). |
| 47 |
|
| 48 |
# Show the Dialog whenever you need. This is non-blocking: |
| 49 |
open_dialog.show() |
| 50 |
""" |
| 51 |
|
| 52 |
|
| 53 |
class _Dialog(_EventDispatcher): |
| 54 |
"""Dialog base class |
| 55 |
|
| 56 |
This base class sets up a ProcessPoolExecutor with a single |
| 57 |
background Process. This allows the Dialog to display in |
| 58 |
the background without blocking or interfering with the main |
| 59 |
application Process. This also limits to a single open Dialog |
| 60 |
at a time. |
| 61 |
""" |
| 62 |
|
| 63 |
executor = _ProcessPoolExecutor(max_workers=1) |
| 64 |
_dialog = None |
| 65 |
|
| 66 |
@staticmethod |
| 67 |
def _open_dialog(dialog): |
| 68 |
import tkinter as tk |
| 69 |
root = tk.Tk() |
| 70 |
root.withdraw() |
| 71 |
return dialog.show() |
| 72 |
|
| 73 |
def open(self): |
| 74 |
future = self.executor.submit(self._open_dialog, self._dialog) |
| 75 |
future.add_done_callback(self._dispatch_event) |
| 76 |
|
| 77 |
def _dispatch_event(self, future): |
| 78 |
raise NotImplementedError |
| 79 |
|
| 80 |
|
| 81 |
class FileOpenDialog(_Dialog): |
| 82 |
def __init__(self, title="Open File", initial_dir=_os.path.curdir, filetypes=None, multiple=False): |
| 83 |
""" |
| 84 |
:Parameters: |
| 85 |
`title` : str |
| 86 |
The Dialog Window name. Defaults to "Open File". |
| 87 |
`initial_dir` : str |
| 88 |
The directory to start in. |
| 89 |
`filetypes` : list of tuple |
| 90 |
An optional list of tuples containing (name, extension) to filter by. |
| 91 |
If none are given, all files will be shown and selectable. |
| 92 |
For example: `[("PNG", ".png"), ("24-bit Bitmap", ".bmp")]` |
| 93 |
`multiple` : bool |
| 94 |
True if multiple files can be selected. Defaults to False. |
| 95 |
""" |
| 96 |
from tkinter import filedialog |
| 97 |
self._dialog = filedialog.Open(title=title, |
| 98 |
initialdir=initial_dir, |
| 99 |
filetypes=filetypes or (), |
| 100 |
multiple=multiple) |
| 101 |
|
| 102 |
def _dispatch_event(self, future): |
| 103 |
self.dispatch_event('on_dialog_open', future.result()) |
| 104 |
|
| 105 |
def on_dialog_open(self, filenames): |
| 106 |
"""Event for filename choices""" |
| 107 |
|
| 108 |
|
| 109 |
class FileSaveDialog(_Dialog): |
| 110 |
def __init__(self, title="Save As", initial_dir=_os.path.curdir, initial_file=None, filetypes=None, default_ext=""): |
| 111 |
""" |
| 112 |
:Parameters: |
| 113 |
`title` : str |
| 114 |
The Dialog Window name. Defaults to "Save As". |
| 115 |
`initial_dir` : str |
| 116 |
The directory to start in. |
| 117 |
`initial_file` : str |
| 118 |
A default file name to be filled in. Defaults to None. |
| 119 |
`filetypes` : list of tuple |
| 120 |
An optional list of tuples containing (name, extension) to |
| 121 |
filter to. If the `default_ext` argument is not given, this list |
| 122 |
also dictactes the extension that will be added to the entered |
| 123 |
file name. If a list of `filetypes` are not give, you can enter |
| 124 |
any file name to save as. |
| 125 |
For example: `[("PNG", ".png"), ("24-bit Bitmap", ".bmp")]` |
| 126 |
`default_ext` : str |
| 127 |
A default file extension to add to the file. This will override |
| 128 |
the `filetypes` list if given, but will not override a manually |
| 129 |
entered extension. |
| 130 |
""" |
| 131 |
from tkinter import filedialog |
| 132 |
self._dialog = filedialog.SaveAs(title=title, |
| 133 |
initialdir=initial_dir, |
| 134 |
initialfile=initial_file or (), |
| 135 |
filetypes=filetypes or (), |
| 136 |
defaultextension=default_ext) |
| 137 |
|
| 138 |
def _dispatch_event(self, future): |
| 139 |
self.dispatch_event('on_dialog_save', future.result()) |
| 140 |
|
| 141 |
def on_dialog_save(self, filename): |
| 142 |
"""Event for filename choice""" |
| 143 |
|
| 144 |
|
| 145 |
FileOpenDialog.register_event_type('on_dialog_open') |
| 146 |
FileSaveDialog.register_event_type('on_dialog_save') |
| 147 |
|
| 148 |
|
| 149 |
if __name__ == '__main__': |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
save_as = FileSaveDialog(initial_file="test", filetypes=[("PNG", ".png"), ("24-bit Bitmap", ".bmp")]) |
| 156 |
|
| 157 |
@save_as.event |
| 158 |
def on_dialog_save(filename): |
| 159 |
print("FILENAMES ON SAVE!", filename) |
| 160 |
|
| 161 |
save_as.open() |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
open_dialog = FileOpenDialog(filetypes=[("PNG", ".png"), ("24-bit Bitmap", ".bmp")], multiple=True) |
| 168 |
|
| 169 |
@open_dialog.event |
| 170 |
def on_dialog_open(filename): |
| 171 |
print("FILENAMES ON OPEN!", filename) |
| 172 |
|
| 173 |
open_dialog.open() |