zoukankan      html  css  js  c++  java
  • pywinauto进阶练习

    case1.画图工具简单练习

    #_*_coding=utf-8_*_
    import time
    from pprint import pprint
    import logging
    from logging import handlers
    #from pywinauto import actionlogger
    from pywinauto import Application
    # import argparse
    # parser = argparse.ArgumentParser()
    # parser.add_argument("--log", help = "enable logging", type=str, required = False)
    # args = parser.parse_args()
    
    class Logger(object):
        level_relations = {
            'debug':logging.DEBUG,
            'info':logging.INFO,
            'warning':logging.WARNING,
            'error':logging.ERROR,
            'crit':logging.CRITICAL
        }#日志级别关系映射
    
        def __init__(self,logname,filename,level='info',when='D',backCount=3,fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s'):
            self.logger = logging.getLogger(logname)
            format_str = logging.Formatter(fmt)#设置日志格式
            self.logger.setLevel(self.level_relations.get(level))#设置日志级别
            sh = logging.StreamHandler()#往屏幕上输出
            sh.setFormatter(format_str) #设置屏幕上显示的格式
            th = handlers.TimedRotatingFileHandler(filename=filename,when=when,backupCount=backCount,encoding='utf-8')#往文件里写入#指定间隔时间自动生成文件的处理器
            #实例化TimedRotatingFileHandler
            #interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
            # S 秒
            # M 分
            # H 小时、
            # D 天、
            # W 每星期(interval==0时代表星期一)
            # midnight 每天凌晨
            th.setFormatter(format_str)#设置文件里写入的格式
            self.logger.addHandler(sh) #把对象加到logger里
            self.logger.addHandler(th)
    
    
    log=Logger(logname='pywinauto',filename='pywinauto.log',)
    
    app = Application(backend='uia').start(r'mspaint.exe')
    #dlg = app.window(title='无标题-画图')
    dlg = app.window(title_re='.* - 画图')
    #dlg=app['无标题-画图']
    #app['无标题-画图'].draw_outline()
    dlg.draw_outline()
    time.sleep(1)
    dlg.print_control_identifiers()
    dlg['“文件”选项卡'].click()
    time.sleep(1)
    dlg.child_window(title='打开', control_type='MenuItem', found_index=0).invoke()
    time.sleep(1)
    # handle Open dialog
    file_name_edit = dlg['打开Dialog'].child_window(title="文件名(N):", control_type="Edit")
    time.sleep(1)
    file_name_edit.set_text(r'D:pythonstudy2018-4-161.png')
    
    # There are 2 Open buttons:
    # dlg.Open.Open.click() will call drop down list of the file name combo box.
    # The child_window statement is just copied from print_control_identifiers().
    
    #dlg['打开Dialog'].打开Botton.click()
    dlg['打开Dialog'].child_window(title="打开(O)", auto_id="1", control_type="Button").click()
    #new_dlg=app['1.png - 画图Dialog']
    dlg.重新调整大小.click()
    dlg.调整大小和扭曲.像素.select()
    if dlg.调整大小和扭曲.保持纵横比.get_toggle_state() != 1:
        dlg.调整大小和扭曲.保持纵横比.toggle()
    dlg.调整大小和扭曲.child_window(title="水平(H):", control_type="Edit").set_text('90')
    dlg.调整大小和扭曲.确定.click()
    # Select menu "File->Save as->PNG picture"
    dlg['“文件”选项卡'].click()
    dlg.child_window(title="另存为", found_index=1).invoke()
    dlg['保存为'].child_window(title='文件名:',control_type="Edit").set_text('test.png')
    dlg['保存为'].保存.click()
    if dlg.确认另存为.exists():
        dlg.确认另存为.是.click()
    # Close application
    dlg.close()
    View Code

    >>>>>>待续

  • 相关阅读:
    今天面试一些程序员(新,老)手的体会
    UVA 10635 Prince and Princess
    poj 2240 Arbitrage
    poj 2253 Frogger
    poj 2485 Highways
    UVA 11258 String Partition
    UVA 11151 Longest Palindrome
    poj 1125 Stockbroker Grapevine
    poj 1789 Truck History
    poj 3259 Wormholes
  • 原文地址:https://www.cnblogs.com/wuxunyan/p/9386140.html
Copyright © 2011-2022 走看看