OS "Save As", "Select File", & "Choose Directory" Dialogs - python

In my script I want a dialog to pop up in which the user can
select a specific file,
choose a directory,
save a file to a specific directory.
All common OS come with these three dialogs, for example the "Save As" dialog in KDE:
Is there a way of calling a function that:
returns the path to the selected file?
returns the path of the chosen directory?
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.

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.

Related

Set tkinter filedialog to open only executable files

Is there a way to set the tkinter filedialog filetypes attribute to open executable files?
I've been using:
filedialog.askopenfilename(filetypes=[("Applications", "*.exe")],)
but it only opens .exe files, not all types of executable files (eg. .APP).
I could add more attributes to filetypes, but it will not work properly on Linux, where any file can be executed, as long as it has the execute permissions (see here).
How can I set thinter's filedialog to only open any kind of executable file?
I am using Python 3.6.4 on Windows 10 and Python 3.5 on Ubuntu 16.
Any help would be greatly appreciated
There's nothing you can do by using the standard dialogs. Tkinter doesn't support listing files by attributes other than their extensions. You'll have to create your own file selection dialog if you want this feature.

How can I extract the associated icon from any file using Python? I can do it in PowerShell, there must be a way to do it in Python [duplicate]

I'm developing a Python script to do file management on Windows. Essentially, I want to be able to move files to another location, and create in their place a shortcut to a Python script which will do intermediate steps before opening the relocated file. I can currently move files, create functioning shortcuts, and set the shortcut icon image. The only thing I have left is to figure out how to detect the icon which is displayed for the file.
-Need to set the icon for a shortcut (which points to a '.py' script)
-This icon should match that of an arbitrary filetype, exe, etc, which I have the path to
-Hopefully done programmatically through Python. os.system() calls are acceptable. Extra credit is given for not requiring additional modules (though I already have win32com, winshell, pythoncom, etc)
Thanks in advance...
File type information is found in the registry. You could look up the file extension's type with the windows registry module.
Edit: It appears icons are available through shell32, as seen here. The Python equivalent seems to be ctypes.windll.shell32.ExtractIconA, though I'm not sure about the syntax.

Create a desktop icon for a Tkinter script

I have written a python script with a Tkinter GUI. I would like to create a desktop icon that will execute this script so that the end-user (not myself) will be able to double-click the icon and have the GUI load, rather than 'run' the script from the terminal or python shell and then have to F5 from there.
Is there a way to do this? I have googled many arrangements of my question but most answers seem to be normal python scripts, not ones which are Tkinter based.
I am using a Raspberry Pi with Wheezy and Python 2.7
Thanks in advance.
I create executables(icons that I click to start the programs I write) using 'py2exe'. I use windows 7 and I am not sure if it would work for you with Raspberry Pi, but a google search may clear that up for you. I will provide the link below. Basically you end up with a folder with the executable(icon) and also some files, without which the executable won't work. It's unfortunate that you get this extra 'baggage', but it's still handy and the best solution I have come across. I don't think there is a much better way, but I am not 100% on that. Interestingly, I found that you could delete most of these baggage files and the executable would still work, it was trial and error, but it didn't take long. If I want to send the folder to someone, I zip it first.
py2exe is here
If you need a 64 bit binary you can get it here, along with, actually, pretty much every other version. get py2exe here also
Besides creating executable file other option is create simple .bat file:
Open notepad
Enter "C:\ProgramData\Anaconda3\python.exe" "C:\Users\Your ID\script.py"
First part is path to python.exe, second to your python script
save file as .bat file, ex. "open_program.bat"
Now simply double click on saved .bat file icon should open your script.
I like using cx-freeze to convert python scripts to exe.
There is very easy to follow documentation to do this. In short, you create a setup.py script that specifies what libraries and packages you want to include or exclude in your application.
You can then run the build in the console and it will create a build folder, in which will be an Application File. You can create a Desktop short cut using send to and selecting desktop.
Documentation link
You can save the script as a .pyw file so the user can click on the file and the GUi would open

Using askopenfilename in Python on Mac

I'm having a bit of trouble. When I use askopenfilename, it works perfect on Windows and Linux, but Mac seems to be giving me the finger....
Here's an example:
... (Code before all this)
def openFile(self):
self.fileName = askopenfilename(filetypes=[("All Files", "*.*")])
... (Code after all this)
The issue is that when I click the open button I have, this method is invoked, and opens the dialog even on Mac, BUT everything in the dialog is grayed out and will not let me select any files. Why not? And how can I fix this?
So I figured it out. For anyone who may run into this issue, only use filetypes for systems that use file type drop menus. Since Mac does not, just don't include the filetypes tuple.
Example, check for the system running, if it's windows, use filetypes, if mac, don't include it

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories