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.
Related
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.
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.
I want to open a program and interact very briefly with the UI of it. For now I managed to open the program with this line:
subprocess.call([r"C:\Users\path\to\program\program.exe", "first-parameter", "second-parameter"])
The program displays at the start a small warning box with the button "Ok".
I would like to simulate a key press "Enter" so that the program moves on.
I tried to implement it like it was explained in in this question:
import subprocess
import win32com.client as comctl
wsh = comctl.Dispatch("WScript.Shell")
subprocess.call([r"C:\Users\path\to\program\program.exe", "first-parameter", "second-parameter"])
wsh.AppActivate("program.exe")
wsh.SendKeys("{Enter}")
The SendKeys() argument was not reached until I closed the program.exe.
After I closed the program.exe the "Enter" key got pressed.
Is there a way to interact with the UI of program.exe or do the KeyPress() so it gets reached?
To NOT wait for you program to exit you need to use subprocess.Popen instead of subprocess.call. More info in python docs here.
Hope this helps!
This might sound like a really dulled down question but I have honestly searched everywhere for it but is there a way where once the user clicks the "exit" or "stop" button to stop there program right after you click that it will write data to a file somewhere? or would that be impossible since you closesd that program? I honestly don't know, Here's my try at it Its nothing really because I don't entirely know how to do it, but I just say this
if (onExit):
f = open('file.txt', mode='w')
f.write (data)
f.close
my onExit is just a Boolean and yeah I'm just not sure how to do it, I know how dumb that code looks btw I just didn't know how to show to you guys that I have tried looking for it other then if I showed you my history tab
Clicking an 'exit' button typically does not actually close a program immediately. Instead, the code that runs when that button is pushed also takes care of saving data.
If we are talking about a console application, which is 'closed' by ctrl-c (i.e. a KeyboardInterrupt), you can use a try-except block:
try:
raw_input()
except KeyboardInterrupt:
# save here
raise
Python does support atexit handlers, but they are most likely not the right solution to your problem.
If you're using PyDev on Eclipse, the terminate button (red square) sends a kill message to the system, which in turn will kill your program without executing further code.
As the previous answer says, you can use the atexit module, but that only works when your program ends normally.
See also: Is it possible for Eclipse to terminate gently instead of using SIGKILL?
OS = windows 7
I have a python program (works) that is listening to activity on the usb bus. I want to perform a lot of tests that require a particular user input at a particular time. I would like to pop up a window that says, "press button xxx". The key point is that the mainloop needs to continue running because it's looking for events. I don't care about the window or if it remains or not and I don't need to capture any information from the window. I just want a message to the user to press the correct button at the correct time. Any type of signaling would work; it doesn't have to be a gui window. It doesn't have to look pretty. Appreciate any suggestions or links to something like this. thx
It sounds like the operation of the Python script you're running does not depend upon the user input you request. To run another process without interrupting the Python script execution you can use:
import subprocess
subprocess.Popen([exe,arg1,arg2,arg3])
where
exe = executable/script to run from your OS command line
arg1= first argument to pass to exe
arg2= second argument to pass to exe
etc... (as many arguments as your OS supports in a list)
This separate exe process could request input from the user.