Apple Python launcher is acting on command key bindings - python

I've written a Python utility that uses tkinter. I'm running it on a Macintosh. When it is executed, it runs within an apple-supplied Python launcher program (/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app).
My code installs its own menus and I bind to the usual Macintosh command-key equivalents for my edit menu (Command-x, command-c, command-x, command-a, command-z) and for quitting (command-q). My problem is that the Python launcher program is responding to the command key bindings. This is inconvenient for things like pasting because it gets done twice. It's a real problem with quitting because the launcher program kills my program before I can save changed files.
Is there some way I can stop the Python launcher program from acting on command key equivalents? I attempted this: "rootWindow.unbind ('<Command-Key-q>')", but to no avail. The launcher program quits before my code can clean up.
I'm using CPython 3.2 on OS X 10.6.6.

Instead of overriding Tkinter's default key bindings, consider re-mapping Tcl's "exit" command to a custom function. (This is called every time you hit command-q or use the "quit" menu item.)
def save_and_exit():
save_changed_files()
sys.exit()
self.createcommand('exit', save_and_exit)
Besides that, I would recommend removing your copy/paste custom keybinds and letting the library do the work for you. If you're still hell-bent on overriding the defaults, Effbot has a nice tutorial on Tkinter events and bindings.

Is there a specific reason why you are using Python.app for launching? This .app is most likely the reason for misbehaving shortcuts.
If I have understood correctly, this launcher is just a wrapper for default python (/usr/bin/python) with special imports.
If you run from terminal (-v is the key here):
/Library/Frameworks/Python.framework/Versions/5.1.1/Resources/Python.app/Contents/MacOS/Python -v
You will see what it imports at the beginning. Adding these lines to your main file should make the command line launching the same as with the .app.
Note also that python.app is in version 5.1.1.
br,
Juha

First off, /Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app is not Apple-supplied. Most likely you installed Python 3.2 using one of the python.org installers here or from some third-party distributor or possibly you built a framework version from source. In any case, Python.app is a dummy application bundle included in each framework version. Its purpose is to ensure that when you invoke python, even from a command line, it is seen by OS X as a full-fledged GUI application. This is particularly important when using tkinter. The default menus and keybindings you see are supplied by Tcl/Tk, not tkinter. As you've discovered, the right way to go about changing these are to remap the default menus. Be aware that there are currently at least three major variants of Tk available on Mac OS X: Aqua Carbon Tk, Aqua Cocoa Tk, and X11 Tk. There are important details, especially with regards to Mac OS X 10.6, about Python and Tcl/Tk on Mac OS X at the python.org website.

Related

How to detect when terminal is in focus using CPython 3.9?

How is it possible to detect when a terminal (CPython 3.9) is in focus ?
Cant find any solution for that.
In a Python script, I have a global keyboard hook that takes every keyboard-input, where I need it to ignore input when terminal is off focus.
I'm using this package: https://github.com/boppreh/keyboard, that works fine cross-platform (Win32 and Linux) except that it also receives when in off-focus.

How to stop console from poping up when command is called from python GUI?

I have made a GUI for my application. All scripts are in Python (2.7) and the GUI is created with Tkinter. I work on Linux, but I needed this app to be executable on Windows. So I've used py2exe, to create an executable. After a while it is working almost perfectly, but I have the following problem:
Somewhere in the application, I need to call external programs, namely ImageMagick and LaTeX. I use the commands convert, pdflatex, simply by importing os module and running os.system(build), where build = 'convert page.pdf page.gif'etc. When those commands are called from the *.exe application the console pops up (meaning a console window opens up for a split of a second and closes again). Is there a way, to prevent this behaviour?
It does not interrupt the application, but it is ugly and not a desired behaviour.
[Note] I chose not to add any samples, since there are lots of files and other content, which, I think, is not related to the problem. I could however try to post minimal (not)working example. But maybe it is not needed.
Thanks!
import subprocess
subprocess.Popen("application.exe", shell = True)

py2exe: Allow a console window to be either shown or hidden with a sys.argv

I have a python program using PySide. When run normally, it opens up a PySide GUI, but when run with some flags in the command line, it spits some things out in the console window.
I'd like to retain this dual functionality, but it seems with py2exe you have to choose whether to have a console window or not when compiling, with no option for choosing during program execution.
Is what I want to do possible with py2exe, or even with some other python "compiler?"
This is not a py2exe limitation, but a Windows limitation. On Windows, applications are compiled either as Console Applications or GUI Applications. The difference is that Console Applications always open a console window, whilst GUI Applications never do.
As far as I can tell, it's not possible to have an application with dual functionality. As a workaround, I suggest that you simply compile two executables: one for console use and one for GUI use.

Problems with different versions of python

Questions similar to this one have been asked but I haven't seen a solution yet that helps me.
I am running Windows 7 and I've installed two versions of python, 3.3 and 2.7. Python 3.3 was the first version I installed and I was able to run scripts from the desktop (not the command line). I installed python 2.7 so that I could get numpy, scipy, and matplotlib down the road, but I found that all the scripts on my desktop defaulted to python 2.7. Since I coded in 3.3 this caused some issues.
I was able to fix this by right clicking the script icon, browsing programs, navigating to the python 3.3 file in my C: drive, and selecting the idle inside that directory. But then I found I wasn't able to run those scripts with python 2.7 using the exact same procedure.
Unfortunately I don't know command-line programming very much at all, and it seemed like most of the answers were geared towards that as a solution. Ideally I'd like to specify which python I want to run a script from the desktop, or possibly while I'm editing the script.
If it's relevant I'm running the eric python IDE, version 5.
I'd be happy to do the work of reading something fairly technical and long (like a blog post or PDF), but it won't help me much if it isn't aimed at beginners.
The Windows Python Launcher can automatically detect the Python from the shebang in your file.
Start your Python 3.3 scripts with
#!/usr/bin/env python3
and your Python 2.7 scripts with
#!/usr/bin/env python2.7
If you want to use the system's default Python version, you can just start the file with
#!/usr/bin/env python
This will also work on virtually all Unix systems.
Maybe you should chose another IDE, like PyCharm, it could manage different versions of python easily (and gives tons of other useful features). Perhaps free PyDev could also make it.
Also, using virtualenv — is the best solution here, but it's a "hard way".
The method I use is to have several cmd.exe shortcuts on my desktop, each pointing at a different runpython.bat file, one for each version of python. Here is one command example from a shortcut:
%comspec% /k "C:\QA\Python\QAPYTH3\runpython.bat"
Here is a typical runpython.bat:
#SET PATH=%PATH%;"C:\Python32"
#SET PYTHONPATH=C:\Python32\Lib
#ASSOC .py=Python.File
#ASSOC .pyc=Python.CompiledFile
#ASSOC .pyo=Python.CompiledFile
#ASSOC .pyw=Python.NoConFile
#FTYPE Python.CompiledFile="C:\Python32\python.exe" "%%1" %%*
#FTYPE Python.File="C:\Python32\python.exe" "%%1" %%*
#FTYPE Python.NoConFile="C:\Python32\pythonw.exe" "%%1" %%*
#SET PATHEXT=.py;%PATHEXT%
Of course you don't have to call your .bat files the same as I have. Just adjust to suite your setup.

How can I get Emacs' key bindings in Python's IDLE?

I use Emacs primarily for coding Python but sometimes I use IDLE. Is there a way to change the key bindings easily in IDLE to match Emacs?
IDLE provides Emacs keybindings without having to install other software.
Open up the menu item Options -> Configure IDLE...
Go to Keys tab
In the drop down menu on the right
side of the dialog change the select
to "IDLE Classic Unix"
It's not the true emacs key bindings but you get the basics like movement, saving/opening, ...
There's a program for Windows called XKeymacs that allows you to specify emacs keybindings for different programs. It should work with IDLE.
http://www.cam.hi-ho.ne.jp/oishi/indexen.html
-Mark
'readline' module supposedly provides Emacs like key bindings and even functionality. However, it is not available on Windows but on Unix. Therefore, this might be a viable solution if you are not using Windows.
import readline
Since I am running IDLE on Windows it is unfortunately not an option for me.

Categories