Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to implement a file-chooser dialog? #38

Open
soaroc opened this issue Jun 30, 2020 · 2 comments
Open

how to implement a file-chooser dialog? #38

soaroc opened this issue Jun 30, 2020 · 2 comments

Comments

@soaroc
Copy link

soaroc commented Jun 30, 2020

Is there any idea to implement a file-chooser dialog? In specific:

  1. how do I know which pathes to list in current dialog?
  2. how do I know which path/file is chosen in the dialog?
@kalekundert
Copy link
Contributor

Currently there is no built-in file-chooser dialog. It'd definitely be a useful widget, though, so I'd be open to a PR if you're interested.

@benmoran56
Copy link

Over on the Discord server we recently came up with a hack using tkinter. It runs in another process, so it won't cause any conflicts. Results are pyglet events. I don't mean to spam, but just in case it's useful I thought I would share:

from concurrent.futures import ProcessPoolExecutor as _ProcessPoolExecutor

from pyglet.event import EventDispatcher as _EventDispatcher

import os


class _Dialog(_EventDispatcher):
    executor = _ProcessPoolExecutor(max_workers=1)

    @staticmethod
    def _open_dialog():
        raise NotImplementedError

    def open(self):
        future = self.executor.submit(self._open_dialog, self._dialog)
        future.add_done_callback(self._dispatch_event)

    def _dispatch_event(self, future):
        raise NotImplementedError


class FileOpenDialog(_Dialog):
    def __init__(self, title="Open File", initial_dir=os.path.curdir, filetypes="", multiple=False):
        from tkinter import filedialog

        self._dialog = filedialog.Open(title=title,
            initialdir=initial_dir,
            filetypes=filetypes,
            multiple=multiple
        )

    @staticmethod
    def _open_dialog(dialog):
        import tkinter as tk
        root = tk.Tk()
        root.withdraw()
        return dialog.show()

    def _dispatch_event(self, future):
        self.dispatch_event('on_dialog_open', future.result())

    def on_dialog_open(self, filename):
        """Event for filename choice"""
        pass



class FileSaveDialog(_Dialog):
    def __init__(self, title="Save As", initial_dir=os.path.curdir, initial_file="", filetypes="", default_ext=""):
        from tkinter import filedialog
        self._dialog = filedialog.SaveAs(title=title,
            initialdir=initial_dir,
            initialfile=initial_file,
            filetypes=filetypes,
            defaultextension=default_ext,
        )

    @staticmethod
    def _open_dialog(dialog):
        import tkinter as tk
        from tkinter import filedialog
        root = tk.Tk()
        root.withdraw()
        return dialog.show()

    def _dispatch_event(self, future):
        self.dispatch_event('on_dialog_save', future.result())

    def on_dialog_save(self, filename):
        """Event for filename choice"""
        pass



FileOpenDialog.register_event_type('on_dialog_open')
FileSaveDialog.register_event_type('on_dialog_save')


if __name__ == '__main__':
    #save_as = FileSaveDialog(initial_file="test", filetypes=[("PNG", ".png"),("24-bit Bitmap", ".bmp")])
    #save_as.open()


    #@save_as.event
    #def on_dialog_save(filename):
    #    print("FILENAMES ON SAVE!", filename)

    open = FileOpenDialog(filetypes=[("PNG", ".png"),("24-bit Bitmap", ".bmp")], multiple=True)
    open.open()

    @open.event
    def on_dialog_open(filename):
        print("FILENAMES ON OPEN!", filename)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants