Python自动按键QtGUI

2024-04-26 14:08:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含以下部分的python脚本:

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

我要做的是运行可执行文件./test,它将显示一个QtGUI。在这个GUI中,会出现按键提示buton。我想自动按下GUI的这个键,以便可执行文件继续。你知道吗

虽然我的python脚本运行可执行文件,但是GUI提示符出现,直到可执行文件之后才输入按键。有什么办法解决这个问题吗?你知道吗


Tags: keyintest脚本可执行文件forindexreturn
1条回答
网友
1楼 · 发布于 2024-04-26 14:08:32

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子进程都应该保持活动状态,则将其移除。你知道吗

相关问题 更多 >