-1

In my script I want a dialog to pop up in which the user can

  1. select a specific file,
  2. choose a directory,
  3. save a file to a specific directory.

All common OS come with these three dialogs, for example the "Save As" dialog in KDE:

enter image description here

Is there a way of calling a function that:

  1. returns the path to the selected file?
  2. returns the path of the chosen directory?
  3. saves some created file to a chosen directory?

It feels like this should be available, however I am struggling to find any packages that can handle this sort of thing. The package os seemed promissing at first, but no luck. I do not want anything custom-looking using tkinter as is discussed in this thread. That code does, however, do exactly what I want for requirement 2.).

Any hints?

EDIT: I am using Kubuntu 17.10 and Python 2.7.14.

  • The `tkinter` module has a number of dialogs for these sorts of things. `from tkinter.filedialog import askopenfilename`, `import tkinter.messagebox`, `import tkinter.simpledialog`. – martineau Nov 23 '17 at 21:31
  • There's also a third-party module named [`easygui`](http://easygui.sourceforge.net/) that might be all you need. – martineau Nov 23 '17 at 21:34
  • Thanks for your answer. I find the tkinter solution very ugly though and easygui is not much prettier. I was hoping to get the exact same dialog boxes as are provided by the OS. – Douglas James Bock Nov 23 '17 at 21:53
  • I believed this could be done since the dialog boxes across applications are identical, eg. the same "Save As" box for saving .html file in Firefox and a . txt a text editor. – Douglas James Bock Nov 23 '17 at 21:55
  • 2
    On windows and OSX, you _do_ get the same dialogs as provided by the OS with tkinter. – Bryan Oakley Nov 24 '17 at 02:58
  • `easygui` looks the same a tkinter because that's what it's using to implement the functions it provides. One nice thing about it is you don't have to learn tkinter to use it (which is non-trivial) and you don't need to turn your program into a gui application. Essentially it creates little dialog apps on-the-fly and returns the result of the user interaction—the rest of your program can do whatever it wants however it wants. In my option it a better use of your time than writing platform-specific code for every OS you want to support yourself. – martineau Nov 24 '17 at 06:46
  • thanks Bryan, it looks like thats not the case on Ubuntu (KDE)... – Douglas James Bock Nov 24 '17 at 07:44
  • Like I said, they are native on OSX and Windows. – Bryan Oakley Nov 24 '17 at 21:03

1 Answers1

1

you have it in tkinter as

from tkinter.filedialog import askopenfile

for py3, for py2 the import is a bit different, but the docs will let you know. You can get hold of all standard file dialogs this way.

Principally you will need to make a small gui-app with tkinter.

ahed87
  • 1,240
  • 10
  • 10
  • I am aware that it is possible to get this functionality with tkinter (see link). However, their appearance is not very nice. I want the same functionality AND looks as the OS dialogs though. – Douglas James Bock Nov 23 '17 at 21:46
  • well at least on my computer the filedialogues are native, however if you make your own window or use for example the messagedialogue that is of course not native. Please note that you don't really need to show the tkinter window to get the filedialogues to open. Anyways maybe Toga (beeware) is what you are looking for then. – ahed87 Nov 23 '17 at 22:12