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:\pythonstudy\2018-4-16\1.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()
>>>>>>待續