Python - How do I catch clicking Windows' command prompt 'Close' button? - python

When the user clicks the 'X' in the top right of the terminal running the program, I want there to be a confirmation that they really want to exit the program.
So far, the most that I've found is this but that only works with 'ctrl-c', not clicking the 'X'.
I've also found
def on_exit(sig, fun=None):
print("I can't prevent the exit")
win32api.SetConsoleCtrlHandler(on_exit, True)
Which would be perfect if it could prevent the exit.

Related

How do I bring a kill window to the front in tkinter?

I have a python program that opens up in full screen. In the program window is a button that the user is supposed to use when they want to exit the program. This is the function for when they click the button:
def closeProgram():
file_name = os.path.basename(sys.argv[0])
file = open("SaveFile1.txt", "a")
file.write(file_name)
file.close()
exit()
But when I call the exit() function, the kill window that pops up, asking if I REALLY want to stop the program, shows up underneath the program window. Is there way a to bring the kill window to the front?
This is caused by IDLE, not by python or tkinter. Use raise SystemExit instead to kill only your program and not IDLE. Or (much better) redesign your program so that it reaches the end when done. If you are using a tkinter mainloop you can use root.quit() to end it.

How to stop python script

I made script for browser game. In this game, I am standing still and my script clicks on certain places bksononechno, but I need that if I clicked on the letter k This script would kill itself and not try to close the browser and its tab in the browser and something similar.
Sorry for mistakes.
You can add sys.exit(1) in the program itself and do Ctrl-C in the command line.

PyWinAuto: Click() fails until mouse is physically clicked

I'm trying to get PyWinAuto to click a button as soon as it's enabled. Here's what I currently have:
while running:
try:
app = pywinauto.Application().connect(title='Microsoft Outlook', class_name="#32770")['Microsoft Outlook']
app.Allow.Wait('ready', retry_interval=0.1)
app.Allow.Click()
print('Clicked')
except (pywinauto.findbestmatch.MatchError, pywinauto.findwindows.ElementNotFoundError):
time.sleep(0.1)
pass
This works just fine if I start it running after the button is active, clicking and printing 'Clicked' as expected. If I run it before the button is active, it waits for it as expected and then seems to try and click it - Printing 'clicked' repeatedly until I click either mouse button or press enter. If I take the click() out and get it to just return app.Allow then the result is as expected regardless of when I load the script, so it does seem to be click() that's the hangup.
The behaviour is the same regardless of where I click or which window I have active - It'll work if I click anywhere or anything, but it won't do anything at all until I do... Which defeats the object of the automation, really!
Any ideas?
Thanks!
First you have to run the script as Administrator if you use .connect(...). I've already added warning about that and error in the .click() method when target process has higher privileges. See pull request #499. It will be included into coming pywinauto==0.6.5.
There is one more method: .click_input() moves mouse cursor and performs real click. While .click() just sends WM_CLICK window message (might be useful for minimized or non-active window).
P.S. By the way, for Outlook I'd recommend using Application(backend="uia") and you'll have no Win32 API specific problems. See Getting Started Guide about backends difference.

How to close GLUT Window when input_raw() is active? Python

This might be a silly question, but I couldn't figure this out on my own neither have I find the solution on line. I have a PyOpenGl application that uses GLUT to create its window. The program is supposed to continue running while the user input in the TERMINAL is not "quit". Something like this:
command = raw_input()
while command != "quit":
if command == "add_shape":
draw_cube()
elif command == "remove_shape":
clear_window()
elif command == "add_light":
add_light()
command = raw_input()
sys.exit("Application closed by the user")
The problem is that when I can raw_input() closing the window using the "x" button doesn't work, only typing quit would terminate the task. At first I thought GLUT would have a callback function for closing window that I could use to detect the "x" button click and force terminate the application, turns out it does have such a function void glutCloseFunc(void(*)(void)callback) but it is not called while raw_input() is active. So what is a good solution for this? How can I read user input from the terminal and still be able to close my application by clicking the "x" button?
REQUIRED:
Read user input from the terminal.
OPTIONAL:
Using GLUT is optional. I've tried with PyGame, but couldn't find a solution to read from the terminal since it only treats interactions with the window.
Thank you!
I believe the most logic and easy way to do it, is with the use of threads. SO you would have one thread to listen to raw_inputs() and another thread to listen to the window close event.

Arrow key controlled Python menu

I'm trying to create a terminal based program that starts with a main menu. I would like the user to be able to press the up and down keys to highlight each of the options. This can just be done by reprinting the menu. And once this is done the user should be able to press enter to select the option triggering another section of code. At the moment I'm having trouble getting the program to see when I'm pressing the up and down keys. Any help with this world be appreciated.
I am happy to include pygame in the program so long as I don't have to start up the graphical window.
Thanks in advance

Categories

Resources