Using askopenfilename in Python on Mac - python

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

Related

Python IDLE won't open with .py file, only from start menu

I've looked through many similar questions, but no one seems to have had this same problem. I can open the IDLE shell perfectly fine from the start menu. Once in, I can use File->Open to open any .py file.
However, my problem is that if I try to open any .py file from File Explorer with IDLE, the files will run, but the IDLE application won't come up. This especially makes it difficult to quit running when I can't even close the application if it's not open. It also makes it very tedious if I have to always use File->Open from the IDLE shell if I want to open a .py file. How can I at least get the Edit with IDLE option to come back?
I had this same issue and came looking for an answer, but ended up figuring it out:
I had the issue when I downloaded the 64-bit version for windows (3.9.7), but the issue was resolved when I instead installed the 32-bit version of 3.9.7.
Now I can right-click > "Edit with IDLE"
When you open the file from within IDLE, you open it in an edit window. If you try to open it from the file explorer, it will RUN it on IDLE. So that's why you can't just "open it" from the explorer.
I suggest that you start using some other tools to code in python. Sublime Text and Atom are great text editors, and both can be used as a python IDE.
The issue could be outside of Idle as I had the same behavior on OS 12.6. Restarting the Finder from the Force Quit menu resolved.

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

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.

PySide Dialog for files

I want to have the chance to choose a file or a directory, just one not both. For linux and OSX, I have the following code working:
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setViewMode(QFileDialog.Detail)
But, on other computer, where I had installed Windows 7. It doesnot work at same. No idea. Actually, with code above, I can just choose a file, not a directory.
Any suggestion?

How to change default directory for IDLE in windows?

The installation directory is "d:\python2.7", and every time I open IDLE and click on menu File and Open item, the default directory is also "d:\python2.7". So I have to change the directory to where I want.
Is there any way I can change it? Using configuration file or changing environment variable?
I tried to add PYTHONPATH in environment variable, but it doesn't work.
I also import os, and use os.chdir(), but it only changes the working directory, not what I want.
Thank you.
If you're running IDLE from a Windows shortcut, you can just right-click on the shortcut, choose "Properties", and change the field "Start in" to any directory you like.
In response to the answer by Aya:
If you're running IDLE from a Windows shortcut, you can just right-click on the shortcut, choose "Properties", and change the field "Start in" to any directory you like.
Yes you can, however Python IDLE will no longer run. I've found that if the Start In directory for the IDLE shortcut is C:\Python33 or C:\Python33\Lib\idlelib (in my case) that it will still work but if I added a new directory below either of these (for example: myfiles) containing my programs then the IDLE editor fails. In my situation, I've spend days now trying to get Python to easily get to my .py programs by clicking FILE+OPEN in the IDLE editor. Also, I've tried everything to get IDLE to run without failing from the Windows Explorer using right click and selecting "edit with IDLE" but although Idle does opens okay trying to RUN a program from there fails. I did manage to get IDLE to work using SEND TO and until I get an answer to my IDLE issue Post that is what I suppose I will have to do. Hope this helps.
Open the idle or PyShell
press Alt + M or File -> open Module
type in idlelib.IOBinding
the window opens, go to line 185
change either 'dirname = None' or 'filename = None' to the value you desire.
Eventually you need to start the PyShell or the editor with rights to edit this module like
sudo idle
if you can not edit it you need to see idle.pyw in the same directory an trace how IOBinding is used by the idlelib.PyShell.main() function and change the same variable.

.py files not opening in IDLE from OSX Finder?

I recently re-installed Python 2.7.3 from python.org on OSX 10.8.2, and now nothing (literally nothing I can see) happens when I double-click on .py files in Finder, or when I right-click/Open-With/IDLE(2.7.3). (Edit: IDLE is already set as the default program to open .py files; the "right-click" part was just to illustrate I couldn't open them either way.)
I have Tcl/Tk 8.5.13 installed (re-installed after I re-installed Python), and I can open IDLE itself just fine, so this isn't that issue where IDLE won't work without the right version of Tcl/Tk. I can also open .py files from the Terminal using idle -e file.py without a problem.
I did notice that the Python Launcher activates when I open files this way, and is absent when I double-click on .py files in Finder (although again, literally nothing happens when I do this--I have the IDLE icon in my dock and it doesn't start bouncing or anything). Could this be a Python Launcher problem? If not, does anyone know what's going on here?
Update: I tried opening IDLE first and then double-clicking on .py files, and that works. So it seems it's just an issue of IDLE not being opened when you try to open a .py file in the Finder.
I know that means I now have at least three other ways of opening my .py files in IDLE, but I really like just being able to navigate to a file in Finder and open it when IDLE itself is not yet open. So I'd still really like to solve this problem if possible (also I really hate when stuff that's just supposed to work doesn't).
Update: Per Ned Deily's suggestion I checked for errors using Console.app and found:
3/8/13 10:42:38.006 AM com.apple.launchd.peruser.501[276]: ([0x0-0x199199].org.python.IDLE[1975]) Exited with code: 1
pops up whenever I try to double-click a .py file in Finder.
Make sure you have set the intended instance of IDLE as the default application for all .py files. One way to do so is to select a .py file in a Finder window, then select the Finder -> File -> Get Info menu item (or Cmd-I). In the Open with: section, pull down the application menu item and select the desired IDLE; if you have multiple copies of IDLE installed, select the Other.. menu item at the bottom of the list and use the file chooser to select the right version of IDLE. Then, click the Change all... button to Use this application to open all documents like this one. You should now be able to launch a .py file in the desired IDLE by double-clicking. If this doesn't work, examine system.log to see if there are any error messages being logged. You can use the Console utility to examine logs (/Applications/Utilities/Console.app).

Categories