Python自动按下QtGUI

时间:2016-08-29 09:31:45

标签: python automation qtgui xdotool

我有一个python脚本,其中包含以下部分:

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

我想要做的是运行可执行文件./test,它会调出一个QtGUI。在此GUI中,出现了按键提示按钮。我想自动按下GUI的这个按键,以便继续执行可执行文件。

我的python脚本虽然运行可执行文件,但GUI提示符出现并且在可执行文件之后才输入按键。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

在子进程退出之前,

os.system不会返回。 您需要subprocess.Popen。 在发送击键之前sleep一段时间也是一个好主意 (子进程可能需要一些时间才能准备好接受用户输入):

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()

我不需要你最后一行。如果所有 9 子进程都应该保持活动状态 - 请将其删除。