0

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?

user1431515
  • 185
  • 1
  • 10
  • Are you sure that the "Return" key works in executable program? You may first check if the "xdotool" works fine and if the "Return" works in the program. Then, check if the input focus on "test" and tell us the result. – pingze Aug 29 '16 at 10:09

2 Answers2

0

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.

robyschek
  • 1,995
  • 14
  • 19
0

Please try Pyautogui to programmatically control the mouse & keyboard

pip install PyAutoGUI

You may find more details here: https://pyautogui.readthedocs.io/en/latest/quickstart.html#mouse-functions

You can also use ClointFusion, which is developed using Pyautogui among other packages. For more details on ClointFusion, please refer the repository: https://github.com/ClointFusion/ClointFusion#readme

Disclaimer : I am one of the developers of ClointFusion

Mayur Patil
  • 139
  • 2
  • 5