pywinauto実戦-MSペイントの操作

10388 ワード

"""
Example script for MS Paint
    png  ,       
1、   PYTHON pywinauto (      )
Requirements:
  - tested on Windows 10 (should work on Win7+)
  - pywinauto 0.6.1+
"""
from __future__ import print_function
import logging
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()
actionlogger.enable()
logger = logging.getLogger('pywinauto')
if args.log:
    logger.handlers[0] = logging.FileHandler(args.log)
app = Application(backend='uia').start(r'mspaint.exe')
dlg = app.window(title_re='.* -   ')
# File->Open menu selection
dlg["“  ”   "].click()
dlg.child_window(title='  ', control_type='MenuItem', found_index=0).invoke()
#dlg.menu_select(r"  ->  ")
# handle Open dialog
file_name_edit = dlg['  '].child_window(title="   (N):", control_type="Edit")
file_name_edit.set_text(r'D:\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['  '].child_window(title="  (O)", auto_id="1", control_type="Button").click()
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()