利用python控制Qt程序的示例詳解
使用python控制Qt程序,進行文本輸入,按鈕點擊等組件控制
方法一
思路:使用pywin32獲取窗口句柄,獲取窗口位置,根據(jù)組件相對定位與窗口定位得到組件絕對定位,模擬鼠標按下,鍵盤輸入即可
安裝
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開發(fā)的基于微軟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程序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python 實現(xiàn)關聯(lián)規(guī)則算法Apriori的示例
這篇文章主要介紹了python 實現(xiàn)關聯(lián)規(guī)則算法Apriori的示例,幫助大家更好的理解和學習python,感興趣的朋友可以了解下2020-09-09Python實現(xiàn)調用jar或執(zhí)行java代碼的方法詳解
這篇文章主要介紹了Python實現(xiàn)調用jar或執(zhí)行java代碼的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12python神經(jīng)網(wǎng)絡Keras常用學習率衰減匯總
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡Keras常用學習率衰減匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05