当前位置:   article > 正文

Pywinauto 在Windows WPF和Qt 产品上的应用_pywinauto qt

pywinauto qt

目前网络上关于pywinauto的资料大多数是对notepad进行操作。

在公司windows产品上进行了应用。

参考:官方API文档 https://pywinauto.readthedocs.io/en/latest/index.html

 

  1. 1、窗口和控件的选择
  2. # 登录窗口及登录按键
  3. login_dialog=app["Login"]
  4. login_control=login_dialog["Button"]
  5. # datagrid 数据选择
  6. Patient_control = PM_dialog.window(title="LI QIANG",class_name="DataGridCell",found_index=0)
  7. Patient_control.type_keys("{HOME}")
  8. 2、键盘输入
  9. # 窗宽窗位调整,需要先删除窗宽窗位默认值,再输入肺窗值
  10. WL =PM_dialog.window(auto_id="WL",class_name="TextBox")
  11. WL.draw_outline(colour="red")
  12. WL.set_focus()
  13. WL.type_keys("^a"
  14.              "{DELETE}"
  15.              "{VK_SUBTRACT}600" )
  16. 3、鼠标动作
  17. #选择种子点,翻页,再次选择种子点
  18. Seed1_xy=(600,250)
  19. mouse.click(button="left",coords=Seed1_xy)
  20. time.sleep(1)
  21. mouse.scroll(coords=Seed1_xy, wheel_dist=1)
  22. time.sleep(1)
  23. Seed2_xy=(600,350)
  24. mouse.click(button="left",coords=Seed2_xy)
  25. 4、等待窗口出现和消失
  26. Popup= app["MetroProBarWindow"]
  27. Popup.wait("exists",timeout=3, retry_interval=1)
  28. print("正在执行中.......")
  29. Popup.wait_not("exists",timeout=60, retry_interval=1)
  30. print("执行完毕!")
  31. 5、最后校验结果,通过结果文件大小和日期进行判断
  32. assert Organ_Stl_Size > 1.0  and Now_time-Organ_mtime <60.0,"三维模型创建失败
完整代码如下:
  1. from pywinauto.application import Application,findwindows
  2. from pywinauto import mouse
  3. from pywinauto import *
  4. import time
  5. import datetime
  6. from pywinauto import uia_element_info
  7. import subprocess
  8. import os
  9. # 启动程序
  10. # 方法1
  11. # subprocess.Popen(args='D:\CAS_Py_V1.0_R4165_P\Release\CAS_Py_V1.0_R4165_P.exe',cwd=r"D:\CAS_Py_V1.0_R4165_P\Release")
  12. # specify an already running application
  13. # 方法2
  14. app = Application(backend="uia").start(cmd_line="D:\CAS_Py_V1.0_R4165_P\Release\CAS_Py_V1.0_R4165_P.exe",wait_for_idle=False,
  15. work_dir="D:\CAS_Py_V1.0_R4165_P\Release")
  16. # 链接到程序
  17. # app=Application(backend="uia").connect(path=r"D:\CAS_Py_V1.0_R4165_P\Release\CAS_Py_V1.0_R4165_P.exe")
  18. # 登录
  19. # specify a dialog of the application
  20. login_dialog=app["Login"]
  21. login_dialog.draw_outline(colour="red")
  22. # login_dialog.print_control_identifiers()
  23. login_control=login_dialog["Button"]
  24. login_control.draw_outline(colour="red")
  25. login_control.click()
  26. # print(type(app))
  27. # 选择病例
  28. PM_dialog = app["PatientManager"]
  29. # print(type(PM_dialog))
  30. # 标记dialog或者control
  31. PM_dialog.draw_outline(colour="red")
  32. # PM_dialog.print_control_identifiers()
  33. # found_index :The index of the filtered out child element to return
  34. Patient_control = PM_dialog.window(title="LI QIANG",class_name="DataGridCell",found_index=0)
  35. Patient_control.draw_outline(colour="red")
  36. # 控件的类型也是windowspecification
  37. # print(type(Patient_control))
  38. # print("*"*100)
  39. # Patient_control.print_control_identifiers()
  40. # print("*"*100)
  41. # owner control 通过按键进行操作
  42. Patient_control.type_keys("{HOME}")
  43. # # 获取控件坐标位置,进行鼠标点击
  44. # # Menu_3D = PM_dialog.window(title="三维重建",found_index=0)
  45. # # Menu_3D.draw_outline(colour="red")
  46. # # Menu_3D_location=Menu_3D.element_info.rectangle.mid_point()
  47. # # print(Menu_3D_location)
  48. # # mouse.click(button="left",coords=Menu_3D_location)
  49. # # 控件没有title,使用automationID进行定位
  50. # # 切换到三维重建模块
  51. Menu_3D = PM_dialog.window(auto_id="_3dRebuidBtn",class_name="Button")
  52. Menu_3D.draw_outline(colour="red")
  53. Menu_3D.click()
  54. # 切换到肺模块
  55. time.sleep(10)
  56. Organ = PM_dialog.window(auto_id="lung", control_type="Image")
  57. Organ.click_input()
  58. # 窗宽窗位调整,需要先删除窗宽窗位默认值,再输入肺窗值
  59. WL =PM_dialog.window(auto_id="WL",class_name="TextBox")
  60. WL.draw_outline(colour="red")
  61. WL.set_focus()
  62. WL.type_keys("^a"
  63. "{DELETE}"
  64. "{VK_SUBTRACT}600" )
  65. WB =PM_dialog.window(auto_id="WB",class_name="TextBox")
  66. WB.draw_outline(colour="red")
  67. WB.set_focus()
  68. WB.type_keys("^a"
  69. "{DELETE}"
  70. "1400")
  71. WLWB_OK =PM_dialog.window(auto_id=r"w_OK",class_name="Button")
  72. WLWB_OK.draw_outline(colour="red")
  73. WLWB_OK.click_input()
  74. #快速分割
  75. Seg_Seeds=PM_dialog.window(auto_id="segmentation_TakePixel")
  76. Seg_Seeds.draw_outline(colour="red")
  77. Seg_Seeds.click_input()
  78. Seed1_xy=(600,250)
  79. mouse.click(button="left",coords=Seed1_xy)
  80. time.sleep(1)
  81. mouse.scroll(coords=Seed1_xy, wheel_dist=1)
  82. time.sleep(1)
  83. Seed2_xy=(600,350)
  84. mouse.click(button="left",coords=Seed2_xy)
  85. Show=PM_dialog.window(auto_id="segmentation_Show",class_name="Button")
  86. Show.draw_outline(colour="red")
  87. Show.click_input()
  88. # 保存
  89. # 需要处理2个弹出窗口:
  90. # 1.正在执行的窗口 MetroProBarWindow
  91. # 2.保存提示窗口
  92. Popup= app["MetroProBarWindow"]
  93. Popup.wait("exists",timeout=3, retry_interval=1)
  94. print("正在执行中.......")
  95. Popup.wait_not("exists",timeout=60, retry_interval=1)
  96. print("执行完毕!")
  97. Save = PM_dialog.window(auto_id="save",class_name="Button")
  98. Save.draw_outline(colour="red")
  99. Save.click_input()
  100. MessageBox = app["uMessageBox"]
  101. MessageBox.wait("exists",timeout=10, retry_interval=1)
  102. MessageBox.window(title="是", auto_id="tb_YES").click_input()
  103. time.sleep(20)
  104. # 结果校验
  105. Stl_Dir_Path = r"D:\CASImage\20190111\20190111142120_LI QIANG\Results\Stl"
  106. Organ_Name = r"Lung.stl"
  107. Organ_Stl_Path = os.path.join(Stl_Dir_Path,Organ_Name)
  108. Organ_Stl_Size = os.path.getsize(Organ_Stl_Path)/float(1024*1024)
  109. print("三维模型体积为%.2f M"%Organ_Stl_Size)
  110. Organ_mtime = os.path.getmtime(Organ_Stl_Path) #时间戳类型:自1970年1月1日(00:00:00 GMT)以来的秒数
  111. Organ_mtime_str = datetime.datetime.fromtimestamp(Organ_mtime).strftime("%Y-%m-%d %H:%M:%S") #转换为字符串日期时间
  112. Now_time = time.time()
  113. print("三维模型创建时间为:"+Organ_mtime_str)
  114. assert Organ_Stl_Size > 1.0 and Now_time-Organ_mtime <60.0,"三维模型创建失败"

 

视频操作效果如下:

https://pan.baidu.com/s/1Be7dCmCMmPq00FjvDgetpQ

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/121331?site
推荐阅读
相关标签
  

闽ICP备14008679号