Python automate key press of a QtGUI
Question: I have a python script with the section below:

    
    
    for index in range(1,10):
       os.system('./test')
       os.system('xdotool key Return') 
    

What I want to do is to run the executable ./test, which brings up a QtGUI. In
this GUI, a key press prompt buton comes up. I want to automate this key press
of the GUI so that the executable continues.

My python script though runs the executable, the GUI prompt comes up and a key
press is not entered until after the executable. Is there any way to fix this?


Answer: `os.system` doesn't return until the child process exits. You need
`subprocess.Popen`. It's also a good idea to `sleep` a while before sending
keystrokes (it may take some time for the child process to get ready to accept
user input):

    
    
    from subprocess import Popen
    from time import sleep
    
    for index in range(1,10):
       # start the child
       process = Popen('./test')
       # sleep to allow the child to draw its buttons
       sleep(.333)
       # send the keystroke
       os.system('xdotool key Return')
       # wait till the child exits
       process.wait()
    

I'm not shure that you need the last line. If all **9** child processes are
supposed to stay alive -- remove it.

