当前位置: 首页 > news >正文

uiautomation控制计算器,不动鼠标(界面控制)

import os
import uiautomation as auto
import subprocess
import time


class uiautoCalc():
    """uiautomation控制计算器(完全后台操作方案)"""

    def __init__(self):
        auto.uiautomation.DEBUG_SEARCH_TIME = True
        auto.uiautomation.SetGlobalSearchTimeout(2)
        self.calcWindow = auto.WindowControl(searchDepth=1, Name='计算器')
        if not self.calcWindow.Exists(0, 0):
            subprocess.Popen('calc')
            time.sleep(1)
            self.calcWindow = auto.WindowControl(searchDepth=1, Name='计算器')

    def gotoScientific(self):
        """使用快捷键切换到科学计算器"""
        # Alt+1 切换到科学计算器
        self.calcWindow.SendKeys('{Alt}1', waitTime=0.5)
        time.sleep(0.5)
        
        # 清空输入
        self.calcWindow.SendKeys('{Esc}', waitTime=0.1)

    def getKeyControl(self):
        """获取按键映射表(改用SendKeys方式)"""
        key_mapping = {
            '0': '0', '1': '1', '2': '2', '3': '3', '4': '4',
            '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
            '.': '.', '+': '{+}', '-': '{-}', '*': '{*}', '/': '{/}',
            '=': '{=}', '(': '{(}', ')': '{)}'
        }
        return key_mapping

    def calculate(self, expression, key_mapping):
        """完全使用SendKeys计算"""
        expression = ''.join(expression.split())
        if not expression.endswith('='):
            expression += '='
        
        # 发送按键序列
        for char in expression:
            key = key_mapping[char]
            self.calcWindow.SendKeys(key, waitTime=0.05)
        
        # 获取结果
        result = self.calcWindow.TextControl(AutomationId='CalculatorResults')
        return result.Name.replace('显示为', '').strip()

    def calc_demo(self):
        """演示示例"""
        self.gotoScientific()
        key_mapping = self.getKeyControl()
        result = self.calculate('(1 + 2 - 3) * 4 / 5.6 - 7', key_mapping)
        print('(1 + 2 - 3) * 4 / 5.6 - 7 =', result)
        self.calcWindow.CaptureToImage('calc.png')
        self.calcWindow.GetWindowPattern().Close()


if __name__ == "__main__":
    ui = uiautoCalc()
    ui.calc_demo()

http://www.xdnf.cn/news/1071091.html

相关文章:

  • Nuxt.js基础(配置)
  • 【Elasticsearch】Linux环境下安装Elasticsearch
  • 论特定领域软件架构
  • 《Opto-Electronic Advances》热点论文速览(2025)
  • 汽车涂胶车间的“通信桥梁”:PROFIBUS DP转ETHERNET/IP网关的应用实践
  • word中如何保存高清图片,并保存为高质量的pdf文件(图像不失真)
  • 多张图片生成PDF每张图片生成pdf的一页
  • lxd 容器内的深度学习服务器环境配置
  • sql server 将nvarchar长度设置成max有什么隐患
  • VSCode中创建和生成动态库项目
  • 时序数据库全面解析与对比
  • TCP/IP协议简要概述
  • 小型软件开发的三重境界:从混沌编码到结构化设计
  • Stable Diffusion入门-ControlNet 深入理解 第二课:ControlNet模型揭秘与使用技巧
  • 基于残差神经网络的垃圾分类
  • Maven生命周期与阶段扩展深度解析
  • 嵌入式项目:基于QT与Hi3861的物联网智能大棚集成控制系统
  • jenkins中执行python脚本导入路径错误
  • Chrome浏览器访问https提示“您的连接不是私密连接”问题解决方案
  • 【C++特殊工具与技术】固有的不可移植的特性(3)::extern“C“
  • 力扣第455场周赛
  • MATLAB 4D作图
  • Hyperledger Fabric 入门笔记(二十)Fabric V2.5 测试网络进阶之Tape性能测试
  • OpenCV模版匹配方法的衡量指标比较
  • 修复opensuse 风滚草rabbitmq的Error: :plugins_dir_does_not_exist问题
  • 【STM32】外部中断
  • 【Linux】基础开发工具(2)
  • java枚举enum的使用示例
  • 大厂测开实习和小厂开发实习怎么选
  • Java设计模式->责任链模式的介绍