terminate python script with key press outside of console window (on windows) - python

I am writing a script to automate stuff in another program (let's call it the main program) on Windows (the main program is only available for Windows). The script reacts to certain visual elements in the main program by clicking on them. Now I would like to add some way of terminating the script while I'm in fullscreen mode in the main program. Right now I have to alt-tab into the console where my script is running and press ctr+c. The problem here is, that if I alt-tab while my script is running, it might click some random stuff on my desktop.
Ideally, I would like to add some key combination that I have to press while I'm in the main program that would terminate (or pause) the script. I tried to do this with pynput.keyboard, but windows defender marks it as a keylogger (which it technically is) and removes my script. Is there a way of doing this without triggering windows defender?
Alternatively, I guess I could automatically pause my script, whenever the active window is not the main program. I am wondering whether this might be the common approach for cases like mine?

Related

How to make a python file run in the background when minimized?

I made a Python program that I do NOT want to turn into .exe or anything else.
However since the program needs python.exe to run, it runs python.exe openly.
I want to know if I can add a piece of code to the program I made so that it runs in the background.
BTW, I do not want to run through cmd.I want it to be a background process automatically.
Basically, I want to add some code into my program so that as soon as it is clicked upon, it runs in background.
I found out the way to do this easily: I just renamed my file.py into file.pyw
Now, it runs without a console. Basically I typed this in the cmd:
ren file.py file.pyw

How can I start a GUI program from Python, but have the process end when the user closes the window?

I want to write a Python script which will start a GUI program (as in, run a binary program with subprocess.run or os.system or something). The script should not block until the program is done, it should start it and then keep running.
I'm doing this on Ubuntu.
I tried subprocess.Popen, but if I run, say subprocess.Popen("gedit"), I get strange behavior. If I open the Ubuntu system monitor (process manager), I can see that the gedit process appears when I run the script, and the gedit window itself opens. But if I close the window, the process doesn't end in the system monitor. The process stays there until my python script exits.
How can I get the behavior I want? The only thing I can think of right now is to just call subprocess.run in a different Python thread, is that the only thing I can do?
Try using subprocess.call. This has worked for me before.
subprocess.call(['command', 'arguments'])
The program should end when the window is closed.
You have to kill the subprocess you've created before you exit the program.
Try this.

How to overcome a startup program delay?

I've been programming with Python and have made an EXE file.
I've added a shortcut to it in the startup folder in order to make it autostart with Windows.
The problem is that it takes some time between when the blank black console appears and the actual run of the program (i.e. it takes time until the program actually starts working).
How do I get rid of the blank black console window? I think it happens because Windows takes time to load the file's folders and libraries.
I want that the black window to be hidden at startup using win32gui.ShowWindow(win, 0), but it takes too long for this to happen. It works fine when launched normally but not when run from the startup directory while Windows is starting.

After showing the output the python window closes immediately

Python version 2.7
I tried a code and it was not giving any error. When I ran the program, It showed the result only for a few milliseconds and then the window closed immediately. Is there any method to display the result permanently and to close the window only at my wish rather than it closing all by itself?
Just use an raw_input() at the end of you script. This will prompt for a string, and you can close the window then by pressing "Enter"
I am assuming that you are running it by double clicking your .py script. Your OS (Windows I assume) will then close the calling 'cmd' terminal once the script has finished, you could circumvent this by starting the terminal manually (just goto start -> execute and enter cmd) to run the script so it won't close automatically.
This happens because your code runs as fast as it can and stops when its finished.
When you open a .py program via double clicking it on a windows OS; it opens the program in a cmd window and when the code is done running it closes the window again. You only specified it to perform some calculation and then stop after that. Therefor in order to keep the program running type raw_input() or if using python 3 input() at the end of your code in order to prevent it from running all the way through until you press a key.

Ending pythonw.exe process on manual exit

I can't seem to find the right google search to figure out something that should be easy.
I am using wxPython to create a GUI, and saving the Python files as .pyw so I don't have a console. Then I am importing another .py file into the main window when a user does an action. Doing so created the pythonw.exe instance that won't close. To be clear, the .pyw that opens if I only open the main console will clear from my processes if I don't open/insert the other .py file from the first file while running.
When I use the program everything is perfectly fine, but when I use the Windows "X" button to close I still get pythonw.exe in my processes, and the file I am printing errors to is locked due to something else using it(the pythonw.exe). What code do I need to use to make sure that python is exiting completely? It also seems to stay whenever I build in a file - exit as well, and it is only staying on the processes if I insert the other module(only done with user input)
Also this may be obvious but if I open the imported file manually it has the same behavior, as in the python.exe closes when I close out of the file.
If you want to exit the application you can do this like this:
import os
os._exit(0)
This works when the normal exit() or reaching the end of the main file does not work becaus of background threads running.
But so you are ending killing all running threads. You should look into the code of the imported file which causes the problem to see if it has threads.

Categories