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

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.

Related

Keyboard library python3

I am trying to do a python script and while the script is running in the background I want it to listen at some buttons, the problem is that I can't even this simple program to get to work, I get segmentation fault 11. When I run it I use sudo python3 prog.py.
import keyboard
keyboard.wait('esc')
print("test")
Couple of things:
You don't compile a Python program
The problem is that you don't have permission to access that memory, hence the segfault. The fix depends on your system, on my Mac I just changed the permissions of the terminal (so that it could control my computer).
Also, I just want to mention that a process in the background does not take in data from STDIN, so you may have a problem there depending on what you mean.

Using both tkinter and Python shell windows in a program

Is it considered bad practise/style to run tkinter based Python programs as .py, instead of .pyw, such that both the graphics window and the Python shell window are used? Or it is simply a matter of what works best in a particular situation?
I have tried to search for this, but no luck.

using the python keyboard does not work when made into an EXE.

I have been working on a program that remaps certain keys on a connected keyboard. Everything works when I run the script from either idle or sublime. However, after I use pyinstaller nothing works. It still has the process running when I check task manager but the actual remapping does not work. Here is part of the code.
import keyboard
while True:
keyboard.remap_key ('a','m')

Blank python IDLE window when opening python file on macOS

For some reason, whenever I try to open a python script that has around ~450 lines of code, IDLE's windows simply appears empty and when I try to run it doesn't run anything either... However, if I try to run a smaller program it works for some reason. Does anybody know how I am able to run a python script on my Mac besides IDLE or how to fix the IDLE window being blank whenever I open a file?
To learn how to run Python Python programs on a Mac, read Chapters 1 and 4 of Python Setup and Usage. Running from an IDLE editor is just one way.
IDLE uses the tkinter module which wraps the tcl/tk GUI framework. Did you read and follow the instructions of how to replace the buggy tcl/tk that Apple supplies? Read the entire page before starting! Ignoring this page is the most common reason people have problems with IDLE on the Mac. There are a few others, but I do not remember one that matches your description.

Apple Python launcher is acting on command key bindings

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.

Categories