简体   繁体   English

Python 自动按键 QtGUI

[英]Python automate key press of a QtGUI

I have a python script with the section below:我有一个包含以下部分的 python 脚本:

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.我想要做的是运行可执行文件 ./test,它会显示一个 QtGUI。 In this GUI, a key press prompt buton comes up.在此 GUI 中,会出现一个按键提示按钮。 I want to automate this key press of the GUI so that the executable continues.我想自动化 GUI 的这个按键,以便可执行文件继续。

My python script though runs the executable, the GUI prompt comes up and a key press is not entered until after the executable.我的 python 脚本虽然运行可执行文件,但出现 GUI 提示并且直到可执行文件之后才输入按键。 Is there any way to fix this?有没有什么办法解决这一问题?

os.system doesn't return until the child process exits. 直到子进程退出, os.system才返回。 You need subprocess.Popen . 您需要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): 在发送按键之前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()

I'm not shure that you need the last line. 我不确定您是否需要最后一行。 If all 9 child processes are supposed to stay alive -- remove it. 如果所有9个子进程都应保持活动状态-请将其删除。

Please try Pyautogui !请尝试 Pyautogui !

You may find more details here: https://pyautogui.readthedocs.io/en/latest/quickstart.html#mouse-functions您可以在此处找到更多详细信息: https : //pyautogui.readthedocs.io/en/latest/quickstart.html#mouse-functions

You can also use ClointFusion, which is developed using Pyautogui among other packages.您还可以使用 ClointFusion,它是使用 Pyautogui 和其他软件包开发的。 For more details on ClointFusion: https://github.com/ClointFusion/ClointFusion#readme有关 ClointFusion 的更多详细信息: https : //github.com/ClointFusion/ClointFusion#readme

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM