利用python控制Qt程序的示例详解
使用python控制Qt程序,进行文本输入,按钮点击等组件控制
方法一
思路:使用pywin32获取窗口句柄,获取窗口位置,根据组件相对定位与窗口定位得到组件绝对定位,模拟鼠标按下,键盘输入即可
安装
pip install pywin32
源码
import pyautogui
import win32api
import win32gui
import pyperclip
def findWindow(title):
windows = pyautogui.getWindowsWithTitle(title)
if(len(windows) == 0):
raise Exception("未找到窗口")
return windows[0]
def PushButtonClick(hwd,relatePos):
# 模拟按钮点击
curPosi = win32api.GetCursorPos()
hwdPosi = win32gui.GetWindowRect(hwd)
win32api.SetCursorPos([hwdPosi[0]+relatePos[0],hwdPosi[1]+relatePos[1]])
pyautogui.click()
pyautogui.sleep(0.3)
win32api.SetCursorPos(curPosi)
def LineEditInput(hwd,relatePos,value):
# 模拟输入框输入
curPosi = win32api.GetCursorPos()
hwdPosi = win32gui.GetWindowRect(hwd)
win32api.SetCursorPos([hwdPosi[0] + relatePos[0], hwdPosi[1] + relatePos[1]])
pyautogui.click()
pyperclip.copy(value)
pyautogui.hotkey('ctrl','v')
pyautogui.sleep(0.3)
win32api.SetCursorPos(curPosi)
def main():
hwd = win32gui.FindWindow(None,"Test")
win32gui.SetForegroundWindow(hwd)
LineEditInput(hwd, [140, 70], "测试")
PushButtonClick(hwd,[300,70])
main()效果图

方法二
思路:使用uiautomation进行组件的控制
uiautomation是yinkaisheng开发的基于微软UIAutomation API的一个python模块,支持自动化Win32,MFC,WPF,Modern UI(Metro UI), Qt, IE, Firefox等UI框架
安装
pip install uiautomation
源码
import uiautomation
def getAllControls(control,map):
if len(control.GetChildren()) != 0:
for child in control.GetChildren():
getAllControls(child,map)
if map.get(control.ControlTypeName) != None:
map[control.ControlTypeName].append(control)
else:
map[control.ControlTypeName] = []
map[control.ControlTypeName].append(control)
def main():
control = uiautomation.WindowControl(searchDepth=1,Name="Test")
controlList = {}
getAllControls(control,controlList)
edit = controlList.get("EditControl")[0]
edit.SendKeys("测试")
btn = controlList.get("ButtonControl")[3]
btn.Click()
main()效果图

到此这篇关于利用python控制Qt程序的示例详解的文章就介绍到这了,更多相关python控制Qt程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
详解PyQt5中textBrowser显示print语句输出的简单方法
这篇文章主要介绍了详解PyQt5中textBrowser显示print语句输出的简单方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-08-08
Python Pandas:DataFrame一列切分成多列、分隔符切分选字段方式
这篇文章主要介绍了Python Pandas:DataFrame一列切分成多列、分隔符切分选字段方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-09-09


最新评论