Interact with the UI of an application in python - python

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!

Related

Python Tkinter Restart the program without killing it

I have one simple question. Is there a way to put some button that will when pressed put everything as it was in moment I started program. Thanks.

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.

is it possible to launch a window that does not stop the mainloop in python

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.

KUbuntu, Python and libtcod - console_is_window_closed not working...?

I've done a reasonable amount of coding with the libtcod library, both the C# and python wrappers. My current setup is KUbuntu 14.10, python 2.7.8, and libtcod 1.5.2.
I've made a few programs that work fine, but the latest I've just started doesn't seem to want to allow me to close the console window.
I can send a CTRL+C from the console that I run the program from, and it will close, but, no amount of clicking on the window's "x" button, or Alt+F4s seem to work.
My code is as follows:
'''
justclose.py
'''
import sys
import time
import libtcodpy as libtcod
libtcod.console_set_custom_font(b'lucida12x12_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(50,50, "The ever-present window", False)
libtcod.console_flush()
while not libtcod.console_is_window_closed():
time.sleep(1)
sys.exit
When I run the program, the console comes up, as expected, and sits around waiting for console_is_window_closed to return true, which it never does. I'm not sure where the problem lies. I can run other programs that use the same initialisation code, and same while loop and which respond just fine to me clicking the close button on the console window.
I've tried looking through an strace of the process, but, I'm not sure I'm up to the task of deciphering it. Nothing looked immediately out of the ordinary.
I'd like some advice on how to track down what's going wrong. Thanks.
EDIT: specifically, I'd like to know how I can check that the close window event is propagating at all, and if so, how far, where it's getting trapped/ignored, that sort of thing. When I run through strace, I see absolutely nothing happening when I click the close button. Is there some better way to debug this?
Replace time.sleep(1) with libtcod.console_check_for_keypress(). When the program sleeps 1 millisecond for each iteration, the program can not respond when you press X. It exits when you press CTRL+C because the program receives the SIGINT signal and it exits immediately. Replacing time.sleep(1) with libtcod.console_check_for_keypress() makes the program check the key pressed on the keyboard, if there is one. That way, the program doesn't block the execution.

How to prevent termination of a running program using "ctrl+c" in Linux using python?

I have written a piece of code in python, in which I am asking questions and users should give their input. Sometimes, these questions are difficult for the user to understand(they are non-english). So most of the time they want to copy paste the sentence into google translate. However, since this code is running in the command prompt,they have to select the text and using "right click --> copy" they can copy the text into google translate. Sometimes, by mistake the press "ctrl+c"(it is natural for everyone to use this combination for copying). Doing this will terminate the code, and they have to start over. I need to know I can prevent this from happening. In other words, if they press "ctrl+c" nothing happens and my software doesn't abort.
thanks
import signal
def SigIntHand(SIG, FRM):
print("Please Right click-copy. Ctrl-C does not work on the cmd prompt")
signal.signal(signal.SIGINT, SigIntHand)
or if you want it completely ignored:
import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)
When you hit ctrl+c it sends SIGINT to the running process. You can catch it as described here.
You can find more about the different types of signals here.
If using X, the text is normally copied to the clipboard once it's selected. Just paste it using middle mouse button or Shift+insert.

Categories

Resources